home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / FROMUTS / PERL / Perl / manual < prev    next >
Text File  |  1992-01-13  |  162KB  |  4,591 lines

  1.  
  2.  
  3.  
  4. PERL(1)                  USER COMMANDS                    PERL(1)
  5.  
  6.  
  7.  
  8. NAME
  9.      perl - Practical Extraction and Report Language
  10.  
  11. SYNOPSIS
  12.      perl [options] filename args
  13.  
  14. DESCRIPTION
  15.      Perl is an interpreted language optimized for scanning arbi-
  16.      trary  text  files,  extracting  information from those text
  17.      files, and printing reports based on that information.  It's
  18.      also  a good language for many system management tasks.  The
  19.      language is intended to be practical  (easy  to  use,  effi-
  20.      cient,  complete)  rather  than  beautiful  (tiny,  elegant,
  21.      minimal).  It combines (in  the  author's  opinion,  anyway)
  22.      some  of the best features of C, sed, awk, and sh, so people
  23.      familiar with those languages should have little  difficulty
  24.      with  it.  (Language historians will also note some vestiges
  25.      of csh, Pascal,  and  even  BASIC-PLUS.)  Expression  syntax
  26.      corresponds  quite  closely  to C expression syntax.  Unlike
  27.      most Unix utilities, perl does  not  arbitrarily  limit  the
  28.      size  of your data--if you've got the memory, perl can slurp
  29.      in your whole file as a  single  string.   Recursion  is  of
  30.      unlimited  depth.   And  the hash tables used by associative
  31.      arrays grow as necessary to  prevent  degraded  performance.
  32.      Perl  uses sophisticated pattern matching techniques to scan
  33.      large amounts of data very quickly.  Although optimized  for
  34.      scanning  text, perl can also deal with binary data, and can
  35.      make dbm files look like associative arrays  (where  dbm  is
  36.      available).   Setuid  perl scripts are safer than C programs
  37.      through a dataflow tracing  mechanism  which  prevents  many
  38.      stupid  security  holes.   If  you have a problem that would
  39.      ordinarily use sed or awk or sh, but it exceeds their  capa-
  40.      bilities  or must run a little faster, and you don't want to
  41.      write the silly thing in C, then perl may be for you.  There
  42.      are  also  translators to turn your sed and awk scripts into
  43.      perl scripts.  OK, enough hype.
  44.  
  45.      Upon startup, perl looks for your script in one of the  fol-
  46.      lowing places:
  47.  
  48.      1.  Specified line by line via -e switches  on  the  command
  49.          line.
  50.  
  51.      2.  Contained in the file specified by the first filename on
  52.          the  command line.  (Note that systems supporting the #!
  53.          notation invoke interpreters this way.)
  54.  
  55.      3.  Passed in implicitly  via  standard  input.   This  only
  56.          works  if there are no filename arguments--to pass argu-
  57.          ments to a stdin script you must explicitly specify a  -
  58.          for the script name.
  59.  
  60.  
  61.  
  62.  
  63. Sun Release 4.1           Last change:                          1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. PERL(1)                  USER COMMANDS                    PERL(1)
  71.  
  72.  
  73.  
  74.      After locating your script, perl compiles it to an  internal
  75.      form.   If  the  script is syntactically correct, it is exe-
  76.      cuted.
  77.  
  78.      Options
  79.  
  80.      Note: on first reading this section may not make much  sense
  81.      to you.  It's here at the front for easy reference.
  82.  
  83.      A single-character option may be combined with the following
  84.      option, if any.  This is particularly useful when invoking a
  85.      script using the #! construct which only  allows  one  argu-
  86.      ment.  Example:
  87.  
  88.           #!/usr/bin/perl -spi.bak # same as -s -p -i.bak
  89.           ...
  90.  
  91.      Options include: +  .TP  5  -0digits  specifies  the  record
  92.      separator  ($/) as an octal number.  If there are no digits,
  93.      the null character is the  separator.   Other  switches  may
  94.      precede  or  follow  the digits.  For example, if you have a
  95.      version of find which can print filenames terminated by  the
  96.      null character, you can say this:
  97.  
  98.          find . -name '*.bak' -print0 | perl -n0e unlink
  99.  
  100.      The special value 00 will cause Perl to slurp files in para-
  101.      graph  mode.   The value 0777 will cause Perl to slurp files
  102.      whole since there is no legal character with that value.
  103.  
  104.      -a   turns on autosplit mode when used with a -n or -p.   An
  105.           implicit  split  command to the @F array is done as the
  106.           first thing inside the implicit while loop produced  by
  107.           the -n or -p.
  108.  
  109.                perl -ane 'print pop(@F), "\n";'
  110.  
  111.           is equivalent to
  112.  
  113.                while (<>) {
  114.                     @F = split(' ');
  115.                     print pop(@F), "\n";
  116.                }
  117.  
  118.  
  119.      -c   causes perl to check the syntax of the script and  then
  120.           exit without executing it.
  121.  
  122.      -d   runs the script under the perl debugger.  See the  sec-
  123.           tion on Debugging.
  124.  
  125.      -Dnumber
  126.  
  127.  
  128.  
  129. Sun Release 4.1           Last change:                          2
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. PERL(1)                  USER COMMANDS                    PERL(1)
  137.  
  138.  
  139.  
  140.           sets debugging flags.  To watch how  it  executes  your
  141.           script,  use  -D14.   (This  only works if debugging is
  142.           compiled into your perl.) Another nice value is -D1024,
  143.           which  lists  your  compiled  syntax  tree.   And -D512
  144.           displays compiled regular expressions.
  145.  
  146.      -e commandline
  147.           may be used to enter one line of script.   Multiple  -e
  148.           commands  may be given to build up a multi-line script.
  149.           If -e is  given,  perl  will  not  look  for  a  script
  150.           filename in the argument list.
  151.  
  152.      -iextension
  153.           specifies that files processed by the <> construct  are
  154.           to  be  edited  in-place.  It does this by renaming the
  155.           input file, opening the output file by the  same  name,
  156.           and selecting that output file as the default for print
  157.           statements.  The extension, if supplied,  is  added  to
  158.           the  name of the old file to make a backup copy.  If no
  159.           extension is supplied, no backup is made.  Saying "perl
  160.           -p  -i.bak  -e "s/foo/bar/;" ... " is the same as using
  161.           the script:
  162.  
  163.                #!/usr/bin/perl -pi.bak
  164.                s/foo/bar/;
  165.  
  166.           which is equivalent to
  167.  
  168.                #!/usr/bin/perl
  169.                while (<>) {
  170.                     if ($ARGV ne $oldargv) {
  171.                          rename($ARGV, $ARGV . '.bak');
  172.                          open(ARGVOUT, ">$ARGV");
  173.                          select(ARGVOUT);
  174.                          $oldargv = $ARGV;
  175.                     }
  176.                     s/foo/bar/;
  177.                }
  178.                continue {
  179.                    print;     # this prints to original filename
  180.                }
  181.                select(STDOUT);
  182.  
  183.           except that the -i form doesn't need to  compare  $ARGV
  184.           to  $oldargv to know when the filename has changed.  It
  185.           does, however, use ARGVOUT for the selected filehandle.
  186.           Note  that  STDOUT  is  restored  as the default output
  187.           filehandle after the loop.
  188.  
  189.           You can use eof to locate the end of each  input  file,
  190.           in  case you want to append to each file, or reset line
  191.           numbering (see example under eof).
  192.  
  193.  
  194.  
  195. Sun Release 4.1           Last change:                          3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. PERL(1)                  USER COMMANDS                    PERL(1)
  203.  
  204.  
  205.  
  206.      -Idirectory
  207.           may be used in  conjunction  with  -P  to  tell  the  C
  208.           preprocessor  where  to  look  for  include  files.  By
  209.           default /usr/include and /usr/lib/perl are searched.
  210.  
  211.      -n   causes perl to assume the following  loop  around  your
  212.           script,  which makes it iterate over filename arguments
  213.           somewhat like "sed -n" or awk:
  214.  
  215.                while (<>) {
  216.                     ...       # your script goes here
  217.                }
  218.  
  219.           Note that the lines are not printed by default.  See -p
  220.           to  have  lines  printed.   Here is an efficient way to
  221.           delete all files older than a week:
  222.  
  223.                find . -mtime +7 -print | perl -ne 'chop;unlink;'
  224.  
  225.           This is faster than using  the  -exec  switch  of  find
  226.           because  you  don't  have  to  start a process on every
  227.           filename found.
  228.  
  229.      -p   causes perl to assume the following  loop  around  your
  230.           script,  which makes it iterate over filename arguments
  231.           somewhat like sed:
  232.  
  233.                while (<>) {
  234.                     ...       # your script goes here
  235.                } continue {
  236.                     print;
  237.                }
  238.  
  239.           Note that the  lines  are  printed  automatically.   To
  240.           suppress  printing use the -n switch.  A -p overrides a
  241.           -n switch.
  242.  
  243.      -P   causes your script to be run through the C preprocessor
  244.           before  compilation  by perl.  (Since both comments and
  245.           cpp directives begin with the # character,  you  should
  246.           avoid  starting  comments  with any words recognized by
  247.           the C preprocessor such as "if", "else" or "define".)
  248.  
  249.      -s   enables some rudimentary switch parsing for switches on
  250.           the  command  line after the script name but before any
  251.           filename arguments (or before a --).  Any switch  found
  252.           there  is removed from @ARGV and sets the corresponding
  253.           variable in the  perl  script.   The  following  script
  254.           prints "true" if and only if the script is invoked with
  255.           a -xyz switch.
  256.  
  257.  
  258.  
  259.  
  260.  
  261. Sun Release 4.1           Last change:                          4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. PERL(1)                  USER COMMANDS                    PERL(1)
  269.  
  270.  
  271.  
  272.                #!/usr/bin/perl -s
  273.                if ($xyz) { print "true\n"; }
  274.  
  275.  
  276.      -S   makes perl use the PATH environment variable to  search
  277.           for  the  script  (unless the name of the script starts
  278.           with a slash).  Typically this is used  to  emulate  #!
  279.           startup  on machines that don't support #!, in the fol-
  280.           lowing manner:
  281.  
  282.                #!/usr/bin/perl
  283.                eval "exec /usr/bin/perl -S $0 $*"
  284.                     if $running_under_some_shell;
  285.  
  286.           The system ignores the first line and feeds the  script
  287.           to  /bin/sh,  which proceeds to try to execute the perl
  288.           script as a  shell  script.   The  shell  executes  the
  289.           second  line as a normal shell command, and thus starts
  290.           up the perl interpreter.  On some  systems  $0  doesn't
  291.           always  contain the full pathname, so the -S tells perl
  292.           to search for the  script  if  necessary.   After  perl
  293.           locates  the  script,  it  parses the lines and ignores
  294.           them because the variable $running_under_some_shell  is
  295.           never  true.   A  better  construct  than  $*  would be
  296.           ${1+"$@"}, which handles embedded spaces  and  such  in
  297.           the  filenames, but doesn't work if the script is being
  298.           interpreted by csh.  In order to  start  up  sh  rather
  299.           than  csh, some systems may have to replace the #! line
  300.           with a line containing just a colon, which will be pol-
  301.           itely  ignored  by  perl.   Other systems can't control
  302.           that, and need a totally devious  construct  that  will
  303.           work  under any of csh, sh or perl, such as the follow-
  304.           ing:
  305.  
  306.                eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  307.                & eval 'exec /usr/bin/perl -S $0 $argv:q'
  308.                     if 0;
  309.  
  310.  
  311.      -u   causes perl to dump core after compiling  your  script.
  312.           You  can  then  take this core dump and turn it into an
  313.           executable file by using the undump program  (not  sup-
  314.           plied).   This  speeds  startup  at the expense of some
  315.           disk space (which you can  minimize  by  stripping  the
  316.           executable).   (Still, a "hello world" executable comes
  317.           out to about 200K on my machine.) If you are  going  to
  318.           run your executable as a set-id program then you should
  319.           probably compile it using taintperl rather than  normal
  320.           perl.   If you want to execute a portion of your script
  321.           before dumping, use the dump operator  instead.   Note:
  322.           availability of undump is platform specific and may not
  323.           be available for a specific port of perl.
  324.  
  325.  
  326.  
  327. Sun Release 4.1           Last change:                          5
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. PERL(1)                  USER COMMANDS                    PERL(1)
  335.  
  336.  
  337.  
  338.      -U   allows perl to do  unsafe  operations.   Currently  the
  339.           only "unsafe" operation is the unlinking of directories
  340.           while running as superuser.
  341.  
  342.      -v   prints the version and patchlevel of your perl  execut-
  343.           able.
  344.  
  345.      -w   prints warnings about identifiers  that  are  mentioned
  346.           only  once,  and  scalar variables that are used before
  347.           being set.  Also warns about redefined subroutines, and
  348.           references  to  undefined  filehandles  or  filehandles
  349.           opened readonly that you are attempting  to  write  on.
  350.           Also  warns you if you use == on values that don't look
  351.           like numbers, and if your subroutines recurse more than
  352.           100 deep.
  353.  
  354.      -xdirectory
  355.           tells perl that the script is embedded  in  a  message.
  356.           Leading  garbage will be discarded until the first line
  357.           that starts with #! and  contains  the  string  "perl".
  358.           Any  meaningful  switches  on that line will be applied
  359.           (but only one group of switches, as with normal #! pro-
  360.           cessing).   If a directory name is specified, Perl will
  361.           switch to that directory  before  running  the  script.
  362.           The -x switch only controls the the disposal of leading
  363.           garbage.  The script must be terminated with __END__ if
  364.           there is trailing garbage to be ignored (the script can
  365.           process any or all of the trailing garbage via the DATA
  366.           filehandle if desired).
  367.  
  368.      Data Types and Objects
  369.  
  370.      Perl has three data types: scalars, arrays of  scalars,  and
  371.      associative arrays of scalars.  Normal arrays are indexed by
  372.      number, and associative arrays by string.
  373.  
  374.      The interpretation of operations and values  in  perl  some-
  375.      times  depends on the requirements of the context around the
  376.      operation or value.  There are three major contexts: string,
  377.      numeric  and  array.  Certain operations return array values
  378.      in contexts wanting an array, and scalar  values  otherwise.
  379.      (If this is true of an operation it will be mentioned in the
  380.      documentation for that operation.) Operations  which  return
  381.      scalars  don't  care  whether  the  context is looking for a
  382.      string or a number, but  scalar  variables  and  values  are
  383.      interpreted as strings or numbers as appropriate to the con-
  384.      text.  A scalar is interpreted as TRUE in the boolean  sense
  385.      if  it  is  not  the null string or 0.  Booleans returned by
  386.      operators are 1 for true and 0 or '' (the null  string)  for
  387.      false.
  388.  
  389.  
  390.  
  391.  
  392.  
  393. Sun Release 4.1           Last change:                          6
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. PERL(1)                  USER COMMANDS                    PERL(1)
  401.  
  402.  
  403.  
  404.      There are actually two varieties of null string: defined and
  405.      undefined.   Undefined  null strings are returned when there
  406.      is no real value for something, such as when  there  was  an
  407.      error, or at end of file, or when you refer to an uninitial-
  408.      ized variable or element of an  array.   An  undefined  null
  409.      string  may become defined the first time you access it, but
  410.      prior to that you can use the defined() operator  to  deter-
  411.      mine whether the value is defined or not.
  412.  
  413.      References to scalar variables always begin with  '$',  even
  414.      when referring to a scalar that is part of an array.  Thus:
  415.  
  416.          $days           # a simple scalar variable
  417.          $days[28]       # 29th element of array @days
  418.          $days{'Feb'}    # one value from an associative array
  419.          $#days          # last index of array @days
  420.  
  421.      but entire arrays or array slices are denoted by '@':
  422.  
  423.          @days           # ($days[0], $days[1],... $days[n])
  424.          @days[3,4,5]    # same as @days[3..5]
  425.          @days{'a','c'}  # same as ($days{'a'},$days{'c'})
  426.  
  427.      and entire associative arrays are denoted by '%':
  428.  
  429.          %days           # (key1, val1, key2, val2 ...)
  430.  
  431.      Any of these eight constructs may serve as an  lvalue,  that
  432.      is,  may be assigned to.  (It also turns out that an assign-
  433.      ment is itself an lvalue in certain  contexts--see  examples
  434.      under  s, tr and chop.) Assignment to a scalar evaluates the
  435.      righthand side in a scalar context, while assignment  to  an
  436.      array  or  array  slice  evaluates  the righthand side in an
  437.      array context.
  438.  
  439.      You may  find  the  length  of  array  @days  by  evaluating
  440.      "$#days",  as in csh.  (Actually, it's not the length of the
  441.      array, it's the subscript of the last element,  since  there
  442.      is  (ordinarily) a 0th element.) Assigning to $#days changes
  443.      the length of the array.  Shortening an array by this method
  444.      does  not actually destroy any values.  Lengthening an array
  445.      that was previously shortened recovers the values that  were
  446.      in  those elements.  You can also gain some measure of effi-
  447.      ciency by preextending an array that is going  to  get  big.
  448.      (You  can  also  extend  an array by assigning to an element
  449.      that is off the end of the array.  This differs from assign-
  450.      ing to $#whatever in that intervening values are set to null
  451.      rather than recovered.) You can truncate an  array  down  to
  452.      nothing  by assigning the null list () to it.  The following
  453.      are exactly equivalent
  454.  
  455.           @whatever = ();
  456.  
  457.  
  458.  
  459. Sun Release 4.1           Last change:                          7
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. PERL(1)                  USER COMMANDS                    PERL(1)
  467.  
  468.  
  469.  
  470.           $#whatever = $[ - 1;
  471.  
  472.  
  473.      If you evaluate an array in a scalar context, it returns the
  474.      length of the array.  The following is always true:
  475.  
  476.           @whatever == $#whatever - $[ + 1;
  477.  
  478.  
  479.      Multi-dimensional arrays are not directly supported, but see
  480.      the  discussion of the $; variable later for a means of emu-
  481.      lating multiple subscripts with an associative  array.   You
  482.      could  also  write  a subroutine to turn multiple subscripts
  483.      into a single subscript.
  484.  
  485.      Every data type has its own  namespace.   You  can,  without
  486.      fear  of  conflict, use the same name for a scalar variable,
  487.      an array, an associative array, a filehandle,  a  subroutine
  488.      name,  and/or  a label.  Since variable and array references
  489.      always start with '$', '@', or  '%',  the  "reserved"  words
  490.      aren't  in  fact  reserved  with  respect to variable names.
  491.      (They ARE reserved with respect to labels  and  filehandles,
  492.      however,  which  don't  have  an  initial special character.
  493.      Hint:  you  could  say   open(LOG,'logfile')   rather   than
  494.      open(log,'logfile').    Using   uppercase  filehandles  also
  495.      improves readability and protects  you  from  conflict  with
  496.      future  reserved  words.)  Case IS significant--"FOO", "Foo"
  497.      and "foo" are all different names.  Names which start with a
  498.      letter may also contain digits and underscores.  Names which
  499.      do not start with a letter are  limited  to  one  character,
  500.      e.g.  "$%" or "$$".  (Most of the one character names have a
  501.      predefined significance to perl.  More later.)
  502.  
  503.      Numeric literals are specified in any of the usual  floating
  504.      point or integer formats:
  505.  
  506.          12345
  507.          12345.67
  508.          .23E-10
  509.          0xffff     # hex
  510.          0377  # octal
  511.  
  512.      String literals are delimited by  either  single  or  double
  513.      quotes.   They  work  much  like shell quotes: double-quoted
  514.      string literals are subject to backslash and  variable  sub-
  515.      stitution;  single-quoted strings are not (except for \' and
  516.      \\).  The usual backslash rules apply for making  characters
  517.      such  as  newline,  tab,  etc.   You can also embed newlines
  518.      directly in your strings, i.e. they can end on  a  different
  519.      line  than they begin.  This is nice, but if you forget your
  520.      trailing quote, the error will not be  reported  until  perl
  521.      finds another line containing the quote character, which may
  522.  
  523.  
  524.  
  525. Sun Release 4.1           Last change:                          8
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. PERL(1)                  USER COMMANDS                    PERL(1)
  533.  
  534.  
  535.  
  536.      be much further on in  the  script.   Variable  substitution
  537.      inside  strings is limited to scalar variables, normal array
  538.      values, and array  slices.   (In  other  words,  identifiers
  539.      beginning  with  $  or  @, followed by an optional bracketed
  540.      expression as  a  subscript.)  The  following  code  segment
  541.      prints out "The price is $100."
  542.  
  543.          $Price = '$100';               # not interpreted
  544.          print "The price is $Price.\n";# interpreted
  545.  
  546.      Note that you can put curly brackets around  the  identifier
  547.      to  delimit it from following alphanumerics.  Also note that
  548.      a single quoted string must be separated  from  a  preceding
  549.      word  by a space, since single quote is a valid character in
  550.      an identifier (see Packages).
  551.  
  552.      Two  special  literals  are  __LINE__  and  __FILE__,  which
  553.      represent the current line number and filename at that point
  554.      in your program.  They may only be used as separate  tokens;
  555.      they  will  not  be interpolated into strings.  In addition,
  556.      the token __END__ may be used to indicate the logical end of
  557.      the  script  before  the  actual end of file.  Any following
  558.      text is ignored (but may be read via the  DATA  filehandle).
  559.      The  two  control  characters  ^D  and  ^Z  are synonyms for
  560.      __END__.
  561.  
  562.      A word that doesn't have any  other  interpretation  in  the
  563.      grammar  will  be  treated as if it had single quotes around
  564.      it.  For this purpose, a word consists only of  alphanumeric
  565.      characters  and underline, and must start with an alphabetic
  566.      character.  As with filehandles and labels, a bare word that
  567.      consists  entirely  of lowercase letters risks conflict with
  568.      future reserved words, and if you use the  -w  switch,  Perl
  569.      will warn you about any such words.
  570.  
  571.      Array values are interpolated into double-quoted strings  by
  572.      joining  all  the  elements  of the array with the delimiter
  573.      specified in the $" variable, space by default.   (Since  in
  574.      versions  of  perl  prior  to  3.0 the @ character was not a
  575.      metacharacter in double-quoted strings, the interpolation of
  576.      @array,   $array[EXPR],   @array[LIST],   $array{EXPR},   or
  577.      @array{LIST} only happens if array is  referenced  elsewhere
  578.      in   the  program  or  is  predefined.)  The  following  are
  579.      equivalent:
  580.  
  581.           $temp = join($",@ARGV);
  582.           system "echo $temp";
  583.  
  584.           system "echo @ARGV";
  585.  
  586.      Within search patterns (which  also  undergo  double-quotish
  587.      substitution)  there  is a bad ambiguity:  Is /$foo[bar]/ to
  588.  
  589.  
  590.  
  591. Sun Release 4.1           Last change:                          9
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. PERL(1)                  USER COMMANDS                    PERL(1)
  599.  
  600.  
  601.  
  602.      be interpreted as /${foo}[bar]/ (where [bar] is a  character
  603.      class for the regular expression) or as /${foo[bar]}/ (where
  604.      [bar] is the subscript to array @foo)?  If @foo doesn't oth-
  605.      erwise  exist,  then  it's  obviously a character class.  If
  606.      @foo exists, perl takes a good guess  about  [bar],  and  is
  607.      almost  always  right.  If it does guess wrong, or if you're
  608.      just plain paranoid, you can force the  correct  interpreta-
  609.      tion with curly brackets as above.
  610.  
  611.      A line-oriented form of quoting is based on the shell  here-
  612.      is syntax.  Following a << you specify a string to terminate
  613.      the quoted material, and all  lines  following  the  current
  614.      line  down  to  the  terminating string are the value of the
  615.      item.  The terminating string may be either an identifier (a
  616.      word),  or  some quoted text.  If quoted, the type of quotes
  617.      you use determines the treatment of the  text,  just  as  in
  618.      regular  quoting.   An unquoted identifier works like double
  619.      quotes.  There must be no space between the << and the iden-
  620.      tifier.   (If  you  put a space it will be treated as a null
  621.      identifier, which is valid,  and  matches  the  first  blank
  622.      line--see  Merry  Christmas  example below.) The terminating
  623.      string must appear by itself (unquoted and with no surround-
  624.      ing whitespace) on the terminating line.
  625.  
  626.           print <<EOF;        # same as above
  627.      The price is $Price.
  628.      EOF
  629.  
  630.           print <<"EOF";      # same as above
  631.      The price is $Price.
  632.      EOF
  633.  
  634.           print << x 10;      # null identifier is delimiter
  635.      Merry Christmas!
  636.  
  637.           print <<`EOC`;      # execute commands
  638.      echo hi there
  639.      echo lo there
  640.      EOC
  641.  
  642.           print <<foo, <<bar; # you can stack them
  643.      I said foo.
  644.      foo
  645.      I said bar.
  646.      bar
  647.  
  648.      Array literals are denoted by separating  individual  values
  649.      by commas, and enclosing the list in parentheses:
  650.  
  651.           (LIST)
  652.  
  653.      In a context not requiring an array value, the value of  the
  654.  
  655.  
  656.  
  657. Sun Release 4.1           Last change:                         10
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664. PERL(1)                  USER COMMANDS                    PERL(1)
  665.  
  666.  
  667.  
  668.      array literal is the value of the final element, as in the C
  669.      comma operator.  For example,
  670.  
  671.          @foo = ('cc', '-E', $bar);
  672.  
  673.      assigns the entire array value to array foo, but
  674.  
  675.          $foo = ('cc', '-E', $bar);
  676.  
  677.      assigns the value of variable bar  to  variable  foo.   Note
  678.      that the value of an actual array in a scalar context is the
  679.      length of the array; the following assigns to $foo the value
  680.      3:
  681.  
  682.          @foo = ('cc', '-E', $bar);
  683.          $foo = @foo;         # $foo gets 3
  684.  
  685.      You  may  have  an  optional  comma   before   the   closing
  686.      parenthesis of an array literal, so that you can say:
  687.  
  688.          @foo = (
  689.           1,
  690.           2,
  691.           3,
  692.          );
  693.  
  694.      When a LIST is  evaluated,  each  element  of  the  list  is
  695.      evaluated in an array context, and the resulting array value
  696.      is interpolated into LIST just as if each individual element
  697.      were a member of LIST.  Thus arrays lose their identity in a
  698.      LIST--the list
  699.  
  700.           (@foo,@bar,&SomeSub)
  701.  
  702.      contains all the elements of @foo followed by all  the  ele-
  703.      ments  of @bar, followed by all the elements returned by the
  704.      subroutine named SomeSub.
  705.  
  706.      A list value may also be subscripted like  a  normal  array.
  707.      Examples:
  708.  
  709.           $time = (stat($file))[8];     # stat returns array value
  710.           $digit = ('a','b','c','d','e','f')[$digit-10];
  711.           return (pop(@foo),pop(@foo))[0];
  712.  
  713.  
  714.      Array lists may be assigned to if and only if  each  element
  715.      of the list is an lvalue:
  716.  
  717.          ($a, $b, $c) = (1, 2, 3);
  718.  
  719.          ($map{'red'}, $map{'blue'}, $map{'green'}) = (0x00f, 0x0f0, 0xf00);
  720.  
  721.  
  722.  
  723. Sun Release 4.1           Last change:                         11
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730. PERL(1)                  USER COMMANDS                    PERL(1)
  731.  
  732.  
  733.  
  734.      The final element may be an array or an associative array:
  735.  
  736.          ($a, $b, @rest) = split;
  737.          local($a, $b, %rest) = @_;
  738.  
  739.      You can actually put an array anywhere in the list, but  the
  740.      first  array  in  the  list will soak up all the values, and
  741.      anything after it will get a null value.  This may be useful
  742.      in a local().
  743.  
  744.      An associative array literal contains pairs of values to  be
  745.      interpreted as a key and a value:
  746.  
  747.          # same as map assignment above
  748.          %map = ('red',0x00f,'blue',0x0f0,'green',0xf00);
  749.  
  750.      Array assignment in a scalar context returns the  number  of
  751.      elements produced by the expression on the right side of the
  752.      assignment:
  753.  
  754.           $x = (($foo,$bar) = (3,2,1)); # set $x to 3, not 2
  755.  
  756.  
  757.      There are several other pseudo-literals that you should know
  758.      about.    If  a  string  is  enclosed  by  backticks  (grave
  759.      accents), it first undergoes variable substitution just like
  760.      a  double  quoted  string.  It is then interpreted as a com-
  761.      mand, and the output of that command is  the  value  of  the
  762.      pseudo-literal,  like  in  a  shell.  In a scalar context, a
  763.      single string consisting of all the output is returned.   In
  764.      an  array  context,  an array of values is returned, one for
  765.      each line of output.  (You can set $/  to  use  a  different
  766.      line  terminator.)  The  command  is  executed each time the
  767.      pseudo-literal is evaluated.  The status value of  the  com-
  768.      mand  is  returned  in  $?  (see  Predefined  Names  for the
  769.      interpretation of $?).  Unlike in  csh,  no  translation  is
  770.      done  on  the return data--newlines remain newlines.  Unlike
  771.      in any of the shells, single quotes  do  not  hide  variable
  772.      names  in  the  command  from  interpretation.   To pass a $
  773.      through to the shell you need to hide it with a backslash.
  774.  
  775.      Evaluating a filehandle in angle brackets  yields  the  next
  776.      line  from  that file (newline included, so it's never false
  777.      until EOF, at which time an undefined  value  is  returned).
  778.      Ordinarily  you  must  assign  that value to a variable, but
  779.      there is one situation where an  automatic  assignment  hap-
  780.      pens.   If  (and only if) the input symbol is the only thing
  781.      inside the  conditional  of  a  while  loop,  the  value  is
  782.      automatically assigned to the variable "$_".  (This may seem
  783.      like an odd thing to you, but you'll use  the  construct  in
  784.      almost  every  perl script you write.) Anyway, the following
  785.      lines are equivalent to each other:
  786.  
  787.  
  788.  
  789. Sun Release 4.1           Last change:                         12
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796. PERL(1)                  USER COMMANDS                    PERL(1)
  797.  
  798.  
  799.  
  800.          while ($_ = <STDIN>) { print; }
  801.          while (<STDIN>) { print; }
  802.          for (;<STDIN>;) { print; }
  803.          print while $_ = <STDIN>;
  804.          print while <STDIN>;
  805.  
  806.      The filehandles STDIN, STDOUT  and  STDERR  are  predefined.
  807.      (The  filehandles  stdin,  stdout  and stderr will also work
  808.      except in packages, where they would be interpreted as local
  809.      identifiers  rather than global.) Additional filehandles may
  810.      be created with the open function.
  811.  
  812.      If a <FILEHANDLE> is used in a context that is  looking  for
  813.      an  array,  an  array  consisting  of all the input lines is
  814.      returned, one line per array element.  It's easy to  make  a
  815.      LARGE data space this way, so use with care.
  816.  
  817.      The null filehandle <> is special and can be used to emulate
  818.      the  behavior  of  sed  and awk.  Input from <> comes either
  819.      from standard input, or from each file listed on the command
  820.      line.   Here's how it works: the first time <> is evaluated,
  821.      the ARGV array is checked, and if it is  null,  $ARGV[0]  is
  822.      set to '-', which when opened gives you standard input.  The
  823.      ARGV array is then processed as a list  of  filenames.   The
  824.      loop
  825.  
  826.           while (<>) {
  827.                ...            # code for each line
  828.           }
  829.  
  830.      is equivalent to
  831.  
  832.           unshift(@ARGV, '-') if $#ARGV < $[;
  833.           while ($ARGV = shift) {
  834.                open(ARGV, $ARGV);
  835.                while (<ARGV>) {
  836.                     ...       # code for each line
  837.                }
  838.           }
  839.  
  840.      except that it isn't as cumbersome to say.  It  really  does
  841.      shift  array ARGV and put the current filename into variable
  842.      ARGV.  It also uses filehandle  ARGV  internally.   You  can
  843.      modify  @ARGV  before  the first <> as long as you leave the
  844.      first filename at the beginning of the array.  Line  numbers
  845.      ($.)  continue as if the input was one big happy file.  (But
  846.      see example under eof for how to reset line numbers on  each
  847.      file.)
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855. Sun Release 4.1           Last change:                         13
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862. PERL(1)                  USER COMMANDS                    PERL(1)
  863.  
  864.  
  865.  
  866.      If you want to set @ARGV to your own list of files, go right
  867.      ahead.   If  you want to pass switches into your script, you
  868.      can put a loop on the front like this:
  869.  
  870.           while ($_ = $ARGV[0], /^-/) {
  871.                shift;
  872.               last if /^--$/;
  873.                /^-D(.*)/ && ($debug = $1);
  874.                /^-v/ && $verbose++;
  875.                ...       # other switches
  876.           }
  877.           while (<>) {
  878.                ...       # code for each line
  879.           }
  880.  
  881.      The <> symbol will return FALSE only once.  If you  call  it
  882.      again  after  this it will assume you are processing another
  883.      @ARGV list, and if you haven't set @ARGV,  will  input  from
  884.      STDIN.
  885.  
  886.      If the string inside the angle brackets is a reference to  a
  887.      scalar  variable  (e.g. <$foo>), then that variable contains
  888.      the name of the filehandle to input from.
  889.  
  890.      If the string inside angle brackets is not a filehandle,  it
  891.      is  interpreted  as  a  filename  pattern to be globbed, and
  892.      either an array of filenames or the  next  filename  in  the
  893.      list  is  returned,  depending  on  context.  One level of $
  894.      interpretation is done  first,  but  you  can't  say  <$foo>
  895.      because  that's  an  indirect filehandle as explained in the
  896.      previous paragraph.  You  could  insert  curly  brackets  to
  897.      force interpretation as a filename glob: <${foo}>.  Example:
  898.  
  899.           while (<*.c>) {
  900.                chmod 0644, $_;
  901.           }
  902.  
  903.      is equivalent to
  904.  
  905.           open(foo, "echo *.c | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
  906.           while (<foo>) {
  907.                chop;
  908.                chmod 0644, $_;
  909.           }
  910.  
  911.      In fact, it's currently implemented that way.  (Which  means
  912.      it will not work on filenames with spaces in them unless you
  913.      have /bin/csh on your machine.) Of course, the shortest  way
  914.      to do the above is:
  915.  
  916.           chmod 0644, <*.c>;
  917.  
  918.  
  919.  
  920.  
  921. Sun Release 4.1           Last change:                         14
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928. PERL(1)                  USER COMMANDS                    PERL(1)
  929.  
  930.  
  931.  
  932.      Syntax
  933.  
  934.      A perl script consists of a  sequence  of  declarations  and
  935.      commands.   The only things that need to be declared in perl
  936.      are report formats and subroutines.  See the sections  below
  937.      for  more information on those declarations.  All uninitial-
  938.      ized user-created objects are assumed to start with  a  null
  939.      or 0 value until they are defined by some explicit operation
  940.      such as assignment.  The sequence of  commands  is  executed
  941.      just once, unlike in sed and awk scripts, where the sequence
  942.      of commands is executed for each  input  line.   While  this
  943.      means  that  you must explicitly loop over the lines of your
  944.      input file (or files), it also means you have much more con-
  945.      trol  over  which files and which lines you look at.  (Actu-
  946.      ally, I'm lying--it is possible to do an implicit loop  with
  947.      either the -n or -p switch.)
  948.  
  949.      A declaration can be put anywhere a command can, but has  no
  950.      effect   on   the  execution  of  the  primary  sequence  of
  951.      commands-declarations all take effect at compile time.  Typ-
  952.      ically  all the declarations are put at the beginning or the
  953.      end of the script.
  954.  
  955.      Perl is, for the most part, a free-form language.  (The only
  956.      exception to this is format declarations, for fairly obvious
  957.      reasons.) Comments are indicated by  the  #  character,  and
  958.      extend  to the end of the line.  If you attempt to use /* */
  959.      C comments, it will be interpreted  either  as  division  or
  960.      pattern  matching,  depending  on  the context.  So don't do
  961.      that.
  962.  
  963.      Compound statements
  964.  
  965.      In perl, a sequence of commands may be treated as  one  com-
  966.      mand by enclosing it in curly brackets.  We will call this a
  967.      BLOCK.
  968.  
  969.      The following compound commands may be used to control flow:
  970.  
  971.           if (EXPR) BLOCK
  972.           if (EXPR) BLOCK else BLOCK
  973.           if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
  974.           LABEL while (EXPR) BLOCK
  975.           LABEL while (EXPR) BLOCK continue BLOCK
  976.           LABEL for (EXPR; EXPR; EXPR) BLOCK
  977.           LABEL foreach VAR (ARRAY) BLOCK
  978.           LABEL BLOCK continue BLOCK
  979.  
  980.      Note that, unlike C and Pascal, these are defined  in  terms
  981.      of BLOCKs, not statements.  This means that the curly brack-
  982.      ets are required--no dangling statements  allowed.   If  you
  983.      want  to write conditionals without curly brackets there are
  984.  
  985.  
  986.  
  987. Sun Release 4.1           Last change:                         15
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994. PERL(1)                  USER COMMANDS                    PERL(1)
  995.  
  996.  
  997.  
  998.      several other ways to do it.  The following all do the  same
  999.      thing:
  1000.  
  1001.           if (!open(foo)) { die "Can't open $foo: $!"; }
  1002.           die "Can't open $foo: $!" unless open(foo);
  1003.           open(foo) || die "Can't open $foo: $!"; # foo or bust!
  1004.           open(foo) ? 'hi mom' : die "Can't open $foo: $!";
  1005.                          # a bit exotic, that last one
  1006.  
  1007.  
  1008.      The if  statement  is  straightforward.   Since  BLOCKs  are
  1009.      always  bounded  by curly brackets, there is never any ambi-
  1010.      guity about which if an else goes with.  If you  use  unless
  1011.      in place of if, the sense of the test is reversed.
  1012.  
  1013.      The while statement  executes  the  block  as  long  as  the
  1014.      expression  is true (does not evaluate to the null string or
  1015.      0).  The LABEL is optional, and if present, consists  of  an
  1016.      identifier  followed  by  a colon.  The LABEL identifies the
  1017.      loop for the loop control statements next,  last,  and  redo
  1018.      (see  below).   If  there  is a continue BLOCK, it is always
  1019.      executed  just  before  the  conditional  is  about  to   be
  1020.      evaluated  again,  similarly to the third part of a for loop
  1021.      in C.  Thus it can be used to  increment  a  loop  variable,
  1022.      even when the loop has been continued via the next statement
  1023.      (similar to the C "continue" statement).
  1024.  
  1025.      If the word while is replaced by the word until,  the  sense
  1026.      of the test is reversed, but the conditional is still tested
  1027.      before the first iteration.
  1028.  
  1029.      In either the if or the while  statement,  you  may  replace
  1030.      "(EXPR)"  with  a  BLOCK, and the conditional is true if the
  1031.      value of the last command in that block is true.
  1032.  
  1033.      The for loop works  exactly  like  the  corresponding  while
  1034.      loop:
  1035.  
  1036.           for ($i = 1; $i < 10; $i++) {
  1037.                ...
  1038.           }
  1039.  
  1040.      is the same as
  1041.  
  1042.           $i = 1;
  1043.           while ($i < 10) {
  1044.                ...
  1045.           } continue {
  1046.                $i++;
  1047.           }
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053. Sun Release 4.1           Last change:                         16
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060. PERL(1)                  USER COMMANDS                    PERL(1)
  1061.  
  1062.  
  1063.  
  1064.      The foreach loop iterates over a normal array value and sets
  1065.      the  variable  VAR  to be each element of the array in turn.
  1066.      The variable is implicitly local to the  loop,  and  regains
  1067.      its  former value upon exiting the loop.  The "foreach" key-
  1068.      word is actually identical to the "for" keyword, so you  can
  1069.      use  "foreach" for readability or "for" for brevity.  If VAR
  1070.      is omitted, $_ is set to each value.  If ARRAY is an  actual
  1071.      array  (as  opposed  to  an  expression  returning  an array
  1072.      value), you can modify each element of the array by  modify-
  1073.      ing VAR inside the loop.  Examples:
  1074.  
  1075.           for (@ary) { s/foo/bar/; }
  1076.  
  1077.           foreach $elem (@elements) {
  1078.                $elem *= 2;
  1079.           }
  1080.  
  1081.           for ((10,9,8,7,6,5,4,3,2,1,'BOOM')) {
  1082.                print $_, "\n"; sleep(1);
  1083.           }
  1084.  
  1085.           for (1..15) { print "Merry Christmas\n"; }
  1086.  
  1087.           foreach $item (split(/:[\\\n:]*/, $ENV{'TERMCAP'})) {
  1088.                print "Item: $item\n";
  1089.           }
  1090.  
  1091.  
  1092.      The BLOCK by itself (labeled or not) is equivalent to a loop
  1093.      that  executes  once.  Thus you can use any of the loop con-
  1094.      trol statements in it to leave or restart  the  block.   The
  1095.      continue  block is optional.  This construct is particularly
  1096.      nice for doing case structures.
  1097.  
  1098.           foo: {
  1099.                if (/^abc/) { $abc = 1; last foo; }
  1100.                if (/^def/) { $def = 1; last foo; }
  1101.                if (/^xyz/) { $xyz = 1; last foo; }
  1102.                $nothing = 1;
  1103.           }
  1104.  
  1105.      There is no official switch statement in perl, because there
  1106.      are  already several ways to write the equivalent.  In addi-
  1107.      tion to the above, you could write
  1108.  
  1109.           foo: {
  1110.                $abc = 1, last foo  if /^abc/;
  1111.                $def = 1, last foo  if /^def/;
  1112.                $xyz = 1, last foo  if /^xyz/;
  1113.                $nothing = 1;
  1114.           }
  1115.  
  1116.  
  1117.  
  1118.  
  1119. Sun Release 4.1           Last change:                         17
  1120.  
  1121.  
  1122.  
  1123.  
  1124.  
  1125.  
  1126. PERL(1)                  USER COMMANDS                    PERL(1)
  1127.  
  1128.  
  1129.  
  1130.      or
  1131.  
  1132.           foo: {
  1133.                /^abc/ && do { $abc = 1; last foo; };
  1134.                /^def/ && do { $def = 1; last foo; };
  1135.                /^xyz/ && do { $xyz = 1; last foo; };
  1136.                $nothing = 1;
  1137.           }
  1138.  
  1139.      or
  1140.  
  1141.           foo: {
  1142.                /^abc/ && ($abc = 1, last foo);
  1143.                /^def/ && ($def = 1, last foo);
  1144.                /^xyz/ && ($xyz = 1, last foo);
  1145.                $nothing = 1;
  1146.           }
  1147.  
  1148.      or even
  1149.  
  1150.           if (/^abc/)
  1151.                { $abc = 1; }
  1152.           elsif (/^def/)
  1153.                { $def = 1; }
  1154.           elsif (/^xyz/)
  1155.                { $xyz = 1; }
  1156.           else
  1157.                {$nothing = 1;}
  1158.  
  1159.      As it happens, these  are  all  optimized  internally  to  a
  1160.      switch  structure,  so  perl  jumps  directly to the desired
  1161.      statement, and you needn't worry about perl executing a  lot
  1162.      of  unnecessary  statements  when  you  have  a string of 50
  1163.      elsifs, as long as you are testing the  same  simple  scalar
  1164.      variable  using  ==,  eq, or pattern matching as above.  (If
  1165.      you're curious as to whether the optimizer has done this for
  1166.      a  particular  case statement, you can use the -D1024 switch
  1167.      to list the syntax tree before execution.)
  1168.  
  1169.      Simple statements
  1170.  
  1171.      The only kind of simple statement is an expression evaluated
  1172.      for  its  side effects.  Every expression (simple statement)
  1173.      must be terminated with a semicolon.  Note that this is like
  1174.      C, but unlike Pascal (and awk).
  1175.  
  1176.      Any simple statement may optionally be followed by a  single
  1177.      modifier, just before the terminating semicolon.  The possi-
  1178.      ble modifiers are:
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185. Sun Release 4.1           Last change:                         18
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. PERL(1)                  USER COMMANDS                    PERL(1)
  1193.  
  1194.  
  1195.  
  1196.           if EXPR
  1197.           unless EXPR
  1198.           while EXPR
  1199.           until EXPR
  1200.  
  1201.      The if and unless modifiers  have  the  expected  semantics.
  1202.      The  while and until modifiers also have the expected seman-
  1203.      tics (conditional evaluated first), except when applied to a
  1204.      do-BLOCK  command,  in  which  case  the block executes once
  1205.      before the conditional is evaluated.  This is  so  that  you
  1206.      can write loops like:
  1207.  
  1208.           do {
  1209.                $_ = <STDIN>;
  1210.                ...
  1211.           } until $_ eq ".\n";
  1212.  
  1213.      (See the do operator below.  Note also that the loop control
  1214.      commands  described  later  will NOT work in this construct,
  1215.      since modifiers don't take loop labels.  Sorry.)
  1216.  
  1217.      Expressions
  1218.  
  1219.      Since perl expressions work almost exactly  like  C  expres-
  1220.      sions, only the differences will be mentioned here.
  1221.  
  1222.      Here's what perl has that C doesn't:
  1223.  
  1224.      **      The exponentiation operator.
  1225.  
  1226.      **=     The exponentiation assignment operator.
  1227.  
  1228.      ()      The null list, used to initialize an array to null.
  1229.  
  1230.      .       Concatenation of two strings.
  1231.  
  1232.      .=      The concatenation assignment operator.
  1233.  
  1234.      eq      String equality (== is  numeric  equality).   For  a
  1235.              mnemonic  just  think  of "eq" as a string.  (If you
  1236.              are used to the awk behavior of using == for  either
  1237.              string or numeric equality based on the current form
  1238.              of the comparands, beware!   You  must  be  explicit
  1239.              here.)
  1240.  
  1241.      ne      String inequality (!= is numeric inequality).
  1242.  
  1243.      lt      String less than.
  1244.  
  1245.      gt      String greater than.
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251. Sun Release 4.1           Last change:                         19
  1252.  
  1253.  
  1254.  
  1255.  
  1256.  
  1257.  
  1258. PERL(1)                  USER COMMANDS                    PERL(1)
  1259.  
  1260.  
  1261.  
  1262.      le      String less than or equal.
  1263.  
  1264.      ge      String greater than or equal.
  1265.  
  1266.      cmp     String comparison, returning -1, 0, or 1.
  1267.  
  1268.      <=>     Numeric comparison, returning -1, 0, or 1.
  1269.  
  1270.      =~      Certain operations search or modify the string  "$_"
  1271.              by default.  This operator makes that kind of opera-
  1272.              tion work on some other string.  The right  argument
  1273.              is  a  search pattern, substitution, or translation.
  1274.              The  left  argument  is  what  is  supposed  to   be
  1275.              searched,  substituted, or translated instead of the
  1276.              default "$_".  The return value indicates  the  suc-
  1277.              cess of the operation.  (If the right argument is an
  1278.              expression other than a  search  pattern,  substitu-
  1279.              tion,  or translation, it is interpreted as a search
  1280.              pattern at run time.  This is less efficient than an
  1281.              explicit  search, since the pattern must be compiled
  1282.              every time the expression is  evaluated.)  The  pre-
  1283.              cedence  of  this operator is lower than unary minus
  1284.              and autoincrement/decrement, but higher than  every-
  1285.              thing else.
  1286.  
  1287.      !~      Just like =~ except the return value is negated.
  1288.  
  1289.      x       The repetition operator.  Returns a string  consist-
  1290.              ing of the left operand repeated the number of times
  1291.              specified by the right operand.
  1292.  
  1293.                   print '-' x 80;          # print row of dashes
  1294.                   print '-' x80;      # illegal, x80 is identifier
  1295.  
  1296.                   print "\t" x ($tab/8), ' ' x ($tab%8);  # tab over
  1297.  
  1298.  
  1299.      x=      The repetition assignment operator.
  1300.  
  1301.      ..      The range operator, which is  really  two  different
  1302.              operators  depending  on  the  context.  In an array
  1303.              context, returns an array  of  values  counting  (by
  1304.              ones)  from the left value to the right value.  This
  1305.              is useful for writing "for (1..10)"  loops  and  for
  1306.              doing slice operations on arrays.
  1307.  
  1308.              In a scalar context, ..  returns  a  boolean  value.
  1309.              The  operator  is bistable, like a flip-flop..  Each
  1310.              .. operator maintains its own boolean state.  It  is
  1311.              false  as  long  as its left operand is false.  Once
  1312.              the left operand is true, the range  operator  stays
  1313.              true  until  the  right operand is true, AFTER which
  1314.  
  1315.  
  1316.  
  1317. Sun Release 4.1           Last change:                         20
  1318.  
  1319.  
  1320.  
  1321.  
  1322.  
  1323.  
  1324. PERL(1)                  USER COMMANDS                    PERL(1)
  1325.  
  1326.  
  1327.  
  1328.              the range operator becomes false again.  (It doesn't
  1329.              become  false  till the next time the range operator
  1330.              is evaluated.  It  can  become  false  on  the  same
  1331.              evaluation it became true, but it still returns true
  1332.              once.) The right operand is not evaluated while  the
  1333.              operator  is  in  the  "false"  state,  and the left
  1334.              operand is not evaluated while the  operator  is  in
  1335.              the  "true"  state.   The scalar .. operator is pri-
  1336.              marily intended for doing line number  ranges  after
  1337.              the fashion of sed or awk.  The precedence is a lit-
  1338.              tle lower than || and &&.   The  value  returned  is
  1339.              either  the  null  string  for  false, or a sequence
  1340.              number (beginning with 1) for  true.   The  sequence
  1341.              number  is  reset  for  each range encountered.  The
  1342.              final sequence number in a range has the string 'E0'
  1343.              appended  to  it,  which  doesn't affect its numeric
  1344.              value, but gives you something to search for if  you
  1345.              want  to  exclude the endpoint.  You can exclude the
  1346.              beginning point by waiting for the  sequence  number
  1347.              to  be  greater than 1.  If either operand of scalar
  1348.              .. is static, that operand is implicitly compared to
  1349.              the $. variable, the current line number.  Examples:
  1350.  
  1351.              As a scalar operator:
  1352.                  if (101 .. 200) { print; }     # print 2nd hundred lines
  1353.  
  1354.                  next line if (1 .. /^$/); # skip header lines
  1355.  
  1356.                  s/^/> / if (/^$/ .. eof());    # quote body
  1357.  
  1358.              As an array operator:
  1359.                  for (101 .. 200) { print; }    # print $_ 100 times
  1360.  
  1361.                  @foo = @foo[$[ .. $#foo]; # an expensive no-op
  1362.                  @foo = @foo[$#foo-4 .. $#foo]; # slice last 5 items
  1363.  
  1364.  
  1365.      -x      A file test.  This unary operator  takes  one  argu-
  1366.              ment,  either  a filename or a filehandle, and tests
  1367.              the associated file to  see  if  something  is  true
  1368.              about  it.   If  the  argument is omitted, tests $_,
  1369.              except for -t, which tests STDIN.  It returns 1  for
  1370.              true and '' for false, or the undefined value if the
  1371.              file doesn't exist.  Precedence is higher than logi-
  1372.              cal  and relational operators, but lower than arith-
  1373.              metic operators.  The operator may be any of:
  1374.                   -r   File is readable by effective uid.
  1375.                   -w   File is writable by effective uid.
  1376.                   -x   File is executable by effective uid.
  1377.                   -o   File is owned by effective uid.
  1378.                   -R   File is readable by real uid.
  1379.                   -W   File is writable by real uid.
  1380.  
  1381.  
  1382.  
  1383. Sun Release 4.1           Last change:                         21
  1384.  
  1385.  
  1386.  
  1387.  
  1388.  
  1389.  
  1390. PERL(1)                  USER COMMANDS                    PERL(1)
  1391.  
  1392.  
  1393.  
  1394.                   -X   File is executable by real uid.
  1395.                   -O   File is owned by real uid.
  1396.                   -e   File exists.
  1397.                   -z   File has zero size.
  1398.                   -s   File has non-zero size (returns size).
  1399.                   -f   File is a plain file.
  1400.                   -d   File is a directory.
  1401.                   -l   File is a symbolic link.
  1402.                   -p   File is a named pipe (FIFO).
  1403.                   -S   File is a socket.
  1404.                   -b   File is a block special file.
  1405.                   -c   File is a character special file.
  1406.                   -u   File has setuid bit set.
  1407.                   -g   File has setgid bit set.
  1408.                   -k   File has sticky bit set.
  1409.                   -t   Filehandle is opened to a tty.
  1410.                   -T   File is a text file.
  1411.                   -B   File is a binary file (opposite of -T).
  1412.                   -M   Age of file in days when script started.
  1413.                   -A   Same for access time.
  1414.                   -C   Same for inode change time.
  1415.  
  1416.              The interpretation of the file permission  operators
  1417.              -r,  -R,  -w,  -W,  -x and -X is based solely on the
  1418.              mode of the file and the uids and gids of the  user.
  1419.              There  may be other reasons you can't actually read,
  1420.              write or execute the file.  Also note that, for  the
  1421.              superuser, -r, -R, -w and -W always return 1, and -x
  1422.              and -X return 1 if any execute bit  is  set  in  the
  1423.              mode.  Scripts run by the superuser may thus need to
  1424.              do a stat() in order to determine the actual mode of
  1425.              the  file,  or  temporarily set the uid to something
  1426.              else.
  1427.  
  1428.              Example:
  1429.  
  1430.                   while (<>) {
  1431.                        chop;
  1432.                        next unless -f $_;  # ignore specials
  1433.                        ...
  1434.                   }
  1435.  
  1436.              Note that -s/a/b/ does not do  a  negated  substitu-
  1437.              tion.   Saying  -exp($foo)  still works as expected,
  1438.              however--only single letters following a  minus  are
  1439.              interpreted as file tests.
  1440.  
  1441.              The -T and -B switches work as follows.   The  first
  1442.              block  or so of the file is examined for odd charac-
  1443.              ters such as strange control  codes  or  metacharac-
  1444.              ters.   If too many odd characters (>10%) are found,
  1445.              it's a -B file, otherwise it's a -T file.  Also, any
  1446.  
  1447.  
  1448.  
  1449. Sun Release 4.1           Last change:                         22
  1450.  
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. PERL(1)                  USER COMMANDS                    PERL(1)
  1457.  
  1458.  
  1459.  
  1460.              file  containing  null  in  the  first block is con-
  1461.              sidered a binary file.  If -T or -B  is  used  on  a
  1462.              filehandle,  the  current  stdio  buffer is examined
  1463.              rather than the first block.  Both -T and -B  return
  1464.              TRUE on a null file, or a file at EOF when testing a
  1465.              filehandle.
  1466.  
  1467.      If any of the file tests (or either stat operator) are given
  1468.      the  special  filehandle consisting of a solitary underline,
  1469.      then the stat structure of the previous file test  (or  stat
  1470.      operator) is used, saving a system call.  (This doesn't work
  1471.      with -t, and you need to remember that  lstat  and  -l  will
  1472.      leave  values  in  the stat structure for the symbolic link,
  1473.      not the real file.) Example:
  1474.  
  1475.           print "Can do.\n" if -r $a || -w _ || -x _;
  1476.  
  1477.           stat($filename);
  1478.           print "Readable\n" if -r _;
  1479.           print "Writable\n" if -w _;
  1480.           print "Executable\n" if -x _;
  1481.           print "Setuid\n" if -u _;
  1482.           print "Setgid\n" if -g _;
  1483.           print "Sticky\n" if -k _;
  1484.           print "Text\n" if -T _;
  1485.           print "Binary\n" if -B _;
  1486.  
  1487.  
  1488.      Here is what C has that perl doesn't:
  1489.  
  1490.      unary &     Address-of operator.
  1491.  
  1492.      unary *     Dereference-address operator.
  1493.  
  1494.      (TYPE)      Type casting operator.
  1495.  
  1496.      Like C, perl does a certain amount of expression  evaluation
  1497.      at  compile  time,  whenever  it  determines that all of the
  1498.      arguments to  an  operator  are  static  and  have  no  side
  1499.      effects.   In  particular,  string  concatenation happens at
  1500.      compile time between literals that don't do variable substi-
  1501.      tution.   Backslash  interpretation  also happens at compile
  1502.      time.  You can say
  1503.  
  1504.           'Now is the time for all' . "\n" .
  1505.           'good men to come to.'
  1506.  
  1507.      and this all reduces to one string internally.
  1508.  
  1509.      The autoincrement operator has a little extra built-in magic
  1510.      to it.  If you increment a variable that is numeric, or that
  1511.      has ever been used in a numeric context, you  get  a  normal
  1512.  
  1513.  
  1514.  
  1515. Sun Release 4.1           Last change:                         23
  1516.  
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. PERL(1)                  USER COMMANDS                    PERL(1)
  1523.  
  1524.  
  1525.  
  1526.      increment.   If, however, the variable has only been used in
  1527.      string contexts since it was set, and has a  value  that  is
  1528.      not  null  and  matches the pattern /^[a-zA-Z]*[0-9]*$/, the
  1529.      increment is done as a  string,  preserving  each  character
  1530.      within its range, with carry:
  1531.  
  1532.           print ++($foo = '99');   # prints '100'
  1533.           print ++($foo = 'a0');   # prints 'a1'
  1534.           print ++($foo = 'Az');   # prints 'Ba'
  1535.           print ++($foo = 'zz');   # prints 'aaa'
  1536.  
  1537.      The autodecrement is not magical.
  1538.  
  1539.      The range operator (in an array context) makes  use  of  the
  1540.      magical  autoincrement  algorithm if the minimum and maximum
  1541.      are strings.  You can say
  1542.  
  1543.           @alphabet = ('A' .. 'Z');
  1544.  
  1545.      to get all the letters of the alphabet, or
  1546.  
  1547.           $hexdigit = (0 .. 9, 'a' .. 'f')[$num & 15];
  1548.  
  1549.      to get a hexadecimal digit, or
  1550.  
  1551.           @z2 = ('01' .. '31');  print @z2[$mday];
  1552.  
  1553.      to get dates with leading zeros.  (If the final value speci-
  1554.      fied is not in the sequence that the magical increment would
  1555.      produce, the sequence goes until the  next  value  would  be
  1556.      longer than the final value specified.)
  1557.  
  1558.      The || and && operators differ from C's in that, rather than
  1559.      returning  0  or  1,  they  return the last value evaluated.
  1560.      Thus, a portable way to find out the  home  directory  might
  1561.      be:
  1562.  
  1563.           $home = $ENV{'HOME'} || $ENV{'LOGDIR'} ||
  1564.               (getpwuid($<))[7] || die "You're homeless!\n";
  1565.  
  1566.  
  1567.      Along with the literals and variables mentioned earlier, the
  1568.      operations in the following section can serve as terms in an
  1569.      expression.  Some of these operations  take  a  LIST  as  an
  1570.      argument.   Such  a  list  can consist of any combination of
  1571.      scalar arguments or array values; the array values  will  be
  1572.      included  in  the  list  as  if each individual element were
  1573.      interpolated at that point in the  list,  forming  a  longer
  1574.      single-dimensional array value.  Elements of the LIST should
  1575.      be separated by commas.  If an operation is listed both with
  1576.      and  without  parentheses around its arguments, it means you
  1577.      can either use it as a unary operator or as a function call.
  1578.  
  1579.  
  1580.  
  1581. Sun Release 4.1           Last change:                         24
  1582.  
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. PERL(1)                  USER COMMANDS                    PERL(1)
  1589.  
  1590.  
  1591.  
  1592.      To  use  it  as  a function call, the next token on the same
  1593.      line must be a left parenthesis.  (There may be  intervening
  1594.      white  space.)  Such a function then has highest precedence,
  1595.      as you would expect from a function.   If  any  token  other
  1596.      than  a  left parenthesis follows, then it is a unary opera-
  1597.      tor, with a precedence depending only on  whether  it  is  a
  1598.      LIST  operator  or  not.   LIST  operators  have lowest pre-
  1599.      cedence.   All  other  unary  operators  have  a  precedence
  1600.      greater  than  relational operators but less than arithmetic
  1601.      operators.  See the section on Precedence.
  1602.  
  1603.      /PATTERN/
  1604.              See m/PATTERN/.
  1605.  
  1606.      ?PATTERN?
  1607.              This is just like the /pattern/ search, except  that
  1608.              it  matches  only  once  between  calls to the reset
  1609.              operator.  This is a useful  optimization  when  you
  1610.              only  want  to see the first occurrence of something
  1611.              in each file of a set of files, for instance.   Only
  1612.              ?? patterns local to the current package are reset.
  1613.  
  1614.      accept(NEWSOCKET,GENERICSOCKET)
  1615.              Does the same thing  that  the  accept  system  call
  1616.              does.   Returns  true  if it succeeded, false other-
  1617.              wise.  See example in section on  Interprocess  Com-
  1618.              munication.
  1619.  
  1620.      alarm(SECONDS)
  1621.  
  1622.      alarm SECONDS
  1623.              Arranges to have a SIGALRM delivered to this process
  1624.              after  the  specified  number  of  seconds (minus 1,
  1625.              actually) have elapsed.  Thus, alarm(15) will  cause
  1626.              a  SIGALRM at some point more than 14 seconds in the
  1627.              future.  Only one timer may  be  counting  at  once.
  1628.              Each  call disables the previous timer, and an argu-
  1629.              ment of 0 may be supplied  to  cancel  the  previous
  1630.              timer  without  starting  a  new  one.  The returned
  1631.              value is the amount of time remaining on the  previ-
  1632.              ous timer.
  1633.  
  1634.      atan2(Y,X)
  1635.              Returns the arctangent of Y/X in the  range  -PI  to
  1636.              PI.
  1637.  
  1638.      bind(SOCKET,NAME)
  1639.              Does the same thing that the bind system call  does.
  1640.              Returns true if it succeeded, false otherwise.  NAME
  1641.              should be a packed address of the  proper  type  for
  1642.              the  socket.  See example in section on Interprocess
  1643.              Communication.
  1644.  
  1645.  
  1646.  
  1647. Sun Release 4.1           Last change:                         25
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654. PERL(1)                  USER COMMANDS                    PERL(1)
  1655.  
  1656.  
  1657.  
  1658.      binmode(FILEHANDLE)
  1659.  
  1660.      binmode FILEHANDLE
  1661.              Arranges for the file to be read in "binary" mode in
  1662.              operating  systems  that  distinguish between binary
  1663.              and text files.  Files that are not read  in  binary
  1664.              mode  have CR LF sequences translated to LF on input
  1665.              and LF translated to CR LF on output.   Binmode  has
  1666.              no  effect  under Unix.  If FILEHANDLE is an expres-
  1667.              sion, the value is taken as the name of the filehan-
  1668.              dle.
  1669.  
  1670.      caller(EXPR)
  1671.  
  1672.      caller  Returns the context of the current subroutine call:
  1673.  
  1674.                   ($package,$filename,$line) = caller;
  1675.  
  1676.              With EXPR, returns some extra information  that  the
  1677.              debugger  uses to print a stack trace.  The value of
  1678.              EXPR indicates how  many  call  frames  to  go  back
  1679.              before the current one.
  1680.  
  1681.      chdir(EXPR)
  1682.  
  1683.      chdir EXPR
  1684.              Changes the working directory to EXPR, if  possible.
  1685.              If  EXPR  is  omitted,  changes  to  home directory.
  1686.              Returns 1 upon success, 0  otherwise.   See  example
  1687.              under die.
  1688.  
  1689.      chmod(LIST)
  1690.  
  1691.      chmod LIST
  1692.              Changes the permissions of a  list  of  files.   The
  1693.              first  element  of  the  list  must be the numerical
  1694.              mode.  Returns  the  number  of  files  successfully
  1695.              changed.
  1696.  
  1697.                   $cnt = chmod 0755, 'foo', 'bar';
  1698.                   chmod 0755, @executables;
  1699.  
  1700.  
  1701.      chop(LIST)
  1702.  
  1703.      chop(VARIABLE)
  1704.  
  1705.      chop VARIABLE
  1706.  
  1707.      chop    Chops off the last character of a string and returns
  1708.              the  character  chopped.   It's  used  primarily  to
  1709.              remove the newline from the end of an input  record,
  1710.  
  1711.  
  1712.  
  1713. Sun Release 4.1           Last change:                         26
  1714.  
  1715.  
  1716.  
  1717.  
  1718.  
  1719.  
  1720. PERL(1)                  USER COMMANDS                    PERL(1)
  1721.  
  1722.  
  1723.  
  1724.              but  is  much  more efficient than s/\n// because it
  1725.              neither scans nor copies the string.  If VARIABLE is
  1726.              omitted, chops $_.  Example:
  1727.  
  1728.                   while (<>) {
  1729.                        chop;     # avoid \n on last field
  1730.                        @array = split(/:/);
  1731.                        ...
  1732.                   }
  1733.  
  1734.              You can actually chop  anything  that's  an  lvalue,
  1735.              including an assignment:
  1736.  
  1737.                   chop($cwd = `pwd`);
  1738.                   chop($answer = <STDIN>);
  1739.  
  1740.              If you chop a list, each element is  chopped.   Only
  1741.              the value of the last chop is returned.
  1742.  
  1743.      chown(LIST)
  1744.  
  1745.      chown LIST
  1746.              Changes the owner (and group) of a  list  of  files.
  1747.              The  first  two  elements  of  the  list must be the
  1748.              NUMERICAL uid and gid, in that order.   Returns  the
  1749.              number of files successfully changed.
  1750.  
  1751.                   $cnt = chown $uid, $gid, 'foo', 'bar';
  1752.                   chown $uid, $gid, @filenames;
  1753.  
  1754.  
  1755.  
  1756.  
  1757.  
  1758.  
  1759.  
  1760.  
  1761.  
  1762.  
  1763.  
  1764.  
  1765.  
  1766.  
  1767.  
  1768.  
  1769.  
  1770.  
  1771.  
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777.  
  1778.  
  1779. Sun Release 4.1           Last change:                         27
  1780.  
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786. PERL(1)                  USER COMMANDS                    PERL(1)
  1787.  
  1788.  
  1789.  
  1790.              Here's an example of looking up non-numeric uids:
  1791.  
  1792.                   print "User: ";
  1793.                   $user = <STDIN>;
  1794.                   chop($user);
  1795.                   print "Files: "
  1796.                   $pattern = <STDIN>;
  1797.                   chop($pattern);
  1798.                   open(pass, '/etc/passwd')
  1799.                        || die "Can't open passwd: $!\n";
  1800.                   while (<pass>) {
  1801.                        ($login,$pass,$uid,$gid) = split(/:/);
  1802.                        $uid{$login} = $uid;
  1803.                        $gid{$login} = $gid;
  1804.                   }
  1805.                   @ary = <${pattern}>;     # get filenames
  1806.                   if ($uid{$user} eq '') {
  1807.                        die "$user not in passwd file";
  1808.                   }
  1809.                   else {
  1810.                        chown $uid{$user}, $gid{$user}, @ary;
  1811.                   }
  1812.  
  1813.  
  1814.      chroot(FILENAME)
  1815.  
  1816.      chroot FILENAME
  1817.              Does the same as the system call of that  name.   If
  1818.              you  don't  know what it does, don't worry about it.
  1819.              If FILENAME is omitted, does chroot to $_.
  1820.  
  1821.      close(FILEHANDLE)
  1822.  
  1823.      close FILEHANDLE
  1824.              Closes the file or pipe  associated  with  the  file
  1825.              handle.   You  don't have to close FILEHANDLE if you
  1826.              are immediately going to  do  another  open  on  it,
  1827.              since  open will close it for you.  (See open.) How-
  1828.              ever, an explicit close on an input file resets  the
  1829.              line  counter ($.), while the implicit close done by
  1830.              open does not.  Also, closing a pipe will  wait  for
  1831.              the  process  executing  on the pipe to complete, in
  1832.              case you want to look at  the  output  of  the  pipe
  1833.              afterwards.  Closing a pipe explicitly also puts the
  1834.              status value of the command into $?.  Example:
  1835.  
  1836.                   open(OUTPUT, '|sort >foo');   # pipe to sort
  1837.                   ...  # print stuff to output
  1838.                   close OUTPUT;       # wait for sort to finish
  1839.                   open(INPUT, 'foo'); # get sort's results
  1840.  
  1841.              FILEHANDLE may be an expression  whose  value  gives
  1842.  
  1843.  
  1844.  
  1845. Sun Release 4.1           Last change:                         28
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852. PERL(1)                  USER COMMANDS                    PERL(1)
  1853.  
  1854.  
  1855.  
  1856.              the real filehandle name.
  1857.  
  1858.      closedir(DIRHANDLE)
  1859.  
  1860.      closedir DIRHANDLE
  1861.              Closes a directory opened by opendir().
  1862.  
  1863.      connect(SOCKET,NAME)
  1864.              Does the same thing that  the  connect  system  call
  1865.              does.   Returns  true  if it succeeded, false other-
  1866.              wise.  NAME should  be  a  package  address  of  the
  1867.              proper  type for the socket.  See example in section
  1868.              on Interprocess Communication.
  1869.  
  1870.      cos(EXPR)
  1871.  
  1872.      cos EXPR
  1873.              Returns the cosine of EXPR (expressed  in  radians).
  1874.              If EXPR is omitted takes cosine of $_.
  1875.  
  1876.      crypt(PLAINTEXT,SALT)
  1877.              Encrypts a string exactly like the crypt()  function
  1878.              in  the C library.  Useful for checking the password
  1879.              file for lousy passwords.   Only  the  guys  wearing
  1880.              white hats should do this.
  1881.  
  1882.      dbmclose(ASSOC_ARRAY)
  1883.  
  1884.      dbmclose ASSOC_ARRAY
  1885.              Breaks the binding between a dbm file and an associ-
  1886.              ative  array.   The values remaining in the associa-
  1887.              tive array are meaningless unless you happen to want
  1888.              to  know  what  was  in  the cache for the dbm file.
  1889.              This function is only useful if you have ndbm.
  1890.  
  1891.      dbmopen(ASSOC,DBNAME,MODE)
  1892.              This binds a dbm or  ndbm  file  to  an  associative
  1893.              array.   ASSOC is the name of the associative array.
  1894.              (Unlike normal open, the first  argument  is  NOT  a
  1895.              filehandle,  even though it looks like one).  DBNAME
  1896.              is the name of the database  (without  the  .dir  or
  1897.              .pag extension).  If the database does not exist, it
  1898.              is created with protection  specified  by  MODE  (as
  1899.              modified  by  the  umask).  If your system only sup-
  1900.              ports the older dbm functions, you may only have one
  1901.              dbmopen in your program.  If your system has neither
  1902.              dbm nor  ndbm,  calling  dbmopen  produces  a  fatal
  1903.              error.
  1904.  
  1905.              Values assigned to the associative  array  prior  to
  1906.              the  dbmopen  are  lost.  A certain number of values
  1907.              from the dbm file are cached in memory.  By  default
  1908.  
  1909.  
  1910.  
  1911. Sun Release 4.1           Last change:                         29
  1912.  
  1913.  
  1914.  
  1915.  
  1916.  
  1917.  
  1918. PERL(1)                  USER COMMANDS                    PERL(1)
  1919.  
  1920.  
  1921.  
  1922.              this number is 64, but you can increase it by preal-
  1923.              locating that number of garbage entries in the asso-
  1924.              ciative array before the dbmopen.  You can flush the
  1925.              cache if necessary with the reset command.
  1926.  
  1927.              If you don't have write access to the dbm file,  you
  1928.              can  only  read associative array variables, not set
  1929.              them.  If you want to test whether  you  can  write,
  1930.              either  use  file tests or try setting a dummy array
  1931.              entry inside an eval, which will trap the error.
  1932.  
  1933.              Note that functions such as keys() and values()  may
  1934.              return  huge  array  values  when  used on large dbm
  1935.              files.  You may prefer to use the each() function to
  1936.              iterate over large dbm files.  Example:
  1937.  
  1938.                   # print out history file offsets
  1939.                   dbmopen(HIST,'/usr/lib/news/history',0666);
  1940.                   while (($key,$val) = each %HIST) {
  1941.                        print $key, ' = ', unpack('L',$val), "\n";
  1942.                   }
  1943.                   dbmclose(HIST);
  1944.  
  1945.  
  1946.      defined(EXPR)
  1947.  
  1948.      defined EXPR
  1949.              Returns a boolean value saying  whether  the  lvalue
  1950.              EXPR  has  a  real  value  or  not.  Many operations
  1951.              return the undefined value under exceptional  condi-
  1952.              tions,  such as end of file, uninitialized variable,
  1953.              system error and such.  This function allows you  to
  1954.              distinguish  between  an undefined null string and a
  1955.              defined  null  string  with  operations  that  might
  1956.              return a real null string, in particular referencing
  1957.              elements of an array.  You may also check to see  if
  1958.              arrays  or  subroutines  exist.   Use  on predefined
  1959.              variables is not  guaranteed  to  produce  intuitive
  1960.              results.  Examples:
  1961.  
  1962.                   print if defined $switch{'D'};
  1963.                   print "$val\n" while defined($val = pop(@ary));
  1964.                   die "Can't readlink $sym: $!"
  1965.                        unless defined($value = readlink $sym);
  1966.                   eval '@foo = ()' if defined(@foo);
  1967.                   die "No XYZ package defined" unless defined %_XYZ;
  1968.                   sub foo { defined &bar ? &bar(@_) : die "No bar"; }
  1969.  
  1970.              See also undef.
  1971.  
  1972.  
  1973.  
  1974.  
  1975.  
  1976.  
  1977. Sun Release 4.1           Last change:                         30
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984. PERL(1)                  USER COMMANDS                    PERL(1)
  1985.  
  1986.  
  1987.  
  1988.      delete $ASSOC{KEY}
  1989.              Deletes the specified value from the specified asso-
  1990.              ciative  array.   Returns  the deleted value, or the
  1991.              undefined value if nothing  was  deleted.   Deleting
  1992.              from $ENV{} modifies the environment.  Deleting from
  1993.              an array bound to a dbm file deletes the entry  from
  1994.              the dbm file.
  1995.  
  1996.              The following deletes all the values of an  associa-
  1997.              tive array:
  1998.  
  1999.                   foreach $key (keys %ARRAY) {
  2000.                        delete $ARRAY{$key};
  2001.                   }
  2002.  
  2003.              (But it would be faster to use  the  reset  command.
  2004.              Saying undef %ARRAY is faster yet.)
  2005.  
  2006.      die(LIST)
  2007.  
  2008.      die LIST
  2009.              Outside of an eval, prints  the  value  of  LIST  to
  2010.              STDERR  and  exits  with  the  current  value  of $!
  2011.              (errno).  If $! is 0, exits with the value of ($? >>
  2012.              8)  (`command`  status).   If  ($? >> 8) is 0, exits
  2013.              with 255.  Inside an  eval,  the  error  message  is
  2014.              stuffed  into $@ and the eval is terminated with the
  2015.              undefined value.
  2016.  
  2017.              Equivalent examples:
  2018.  
  2019.                   die "Can't cd to spool: $!\n"
  2020.                        unless chdir '/usr/spool/news';
  2021.  
  2022.                   chdir '/usr/spool/news' || die "Can't cd to spool: $!\n"
  2023.  
  2024.  
  2025.              If the value of EXPR does not end in a newline,  the
  2026.              current script line number and input line number (if
  2027.              any) are also printed, and a  newline  is  supplied.
  2028.              Hint:  sometimes  appending ", stopped" to your mes-
  2029.              sage will cause it to make  better  sense  when  the
  2030.              string  "at  foo line 123" is appended.  Suppose you
  2031.              are running script "canasta".
  2032.  
  2033.                   die "/etc/games is no good";
  2034.                   die "/etc/games is no good, stopped";
  2035.  
  2036.              produce, respectively
  2037.  
  2038.                   /etc/games is no good at canasta line 123.
  2039.                   /etc/games is no good, stopped at canasta line 123.
  2040.  
  2041.  
  2042.  
  2043. Sun Release 4.1           Last change:                         31
  2044.  
  2045.  
  2046.  
  2047.  
  2048.  
  2049.  
  2050. PERL(1)                  USER COMMANDS                    PERL(1)
  2051.  
  2052.  
  2053.  
  2054.              See also exit.
  2055.  
  2056.      do BLOCK
  2057.              Returns  the  value  of  the  last  command  in  the
  2058.              sequence of commands indicated by BLOCK.  When modi-
  2059.              fied by a loop modifier,  executes  the  BLOCK  once
  2060.              before testing the loop condition.  (On other state-
  2061.              ments  the  loop  modifiers  test  the   conditional
  2062.              first.)
  2063.  
  2064.      do SUBROUTINE (LIST)
  2065.              Executes a SUBROUTINE declared by a sub declaration,
  2066.              and   returns  the  value  of  the  last  expression
  2067.              evaluated in SUBROUTINE.  If there is no  subroutine
  2068.              by  that name, produces a fatal error.  (You may use
  2069.              the "defined" operator to determine if a  subroutine
  2070.              exists.)  If you pass arrays as part of LIST you may
  2071.              wish to pass the length of the  array  in  front  of
  2072.              each  array.   (See the section on subroutines later
  2073.              on.) SUBROUTINE may be a scalar variable,  in  which
  2074.              case  the  variable contains the name of the subrou-
  2075.              tine to execute.  The parentheses  are  required  to
  2076.              avoid confusion with the "do EXPR" form.
  2077.  
  2078.              As an alternate form, you may call a  subroutine  by
  2079.              prefixing  the  name with an ampersand: &foo(@args).
  2080.              If you aren't passing any arguments, you don't  have
  2081.              to use parentheses.  If you omit the parentheses, no
  2082.              @_ array is passed to the subroutine.  The & form is
  2083.              also  used to specify subroutines to the defined and
  2084.              undef operators.
  2085.  
  2086.      do EXPR Uses the value of EXPR as a  filename  and  executes
  2087.              the contents of the file as a perl script.  Its pri-
  2088.              mary use is to include subroutines from a perl  sub-
  2089.              routine library.
  2090.  
  2091.                   do 'stat.pl';
  2092.  
  2093.              is just like
  2094.  
  2095.                   eval `cat stat.pl`;
  2096.  
  2097.              except that it's more efficient, more concise, keeps
  2098.              track  of  the  current filename for error messages,
  2099.              and searches all the -I libraries if the file  isn't
  2100.              in the current directory (see also the @INC array in
  2101.              Predefined Names).  It's the same, however, in  that
  2102.              it  does reparse the file every time you call it, so
  2103.              if you are going to use the file inside a  loop  you
  2104.              might  prefer to use -P and #include, at the expense
  2105.              of a little more startup time.   (The  main  problem
  2106.  
  2107.  
  2108.  
  2109. Sun Release 4.1           Last change:                         32
  2110.  
  2111.  
  2112.  
  2113.  
  2114.  
  2115.  
  2116. PERL(1)                  USER COMMANDS                    PERL(1)
  2117.  
  2118.  
  2119.  
  2120.              with #include is that cpp doesn't grok # comments--a
  2121.              workaround is to use ";#" for standalone  comments.)
  2122.              Note that the following are NOT equivalent:
  2123.  
  2124.                   do $foo;  # eval a file
  2125.                   do $foo();     # call a subroutine
  2126.  
  2127.              Note that inclusion of library  routines  is  better
  2128.              done with the "require" operator.
  2129.  
  2130.      dump LABEL
  2131.              This causes an immediate core dump.  Primarily  this
  2132.              is  so  that  you can use the undump program to turn
  2133.              your core dump into an executable binary after  hav-
  2134.              ing  initialized all your variables at the beginning
  2135.              of the program.  When the new binary is executed  it
  2136.              will begin by executing a "goto LABEL" (with all the
  2137.              restrictions that goto suffers).  Think of it  as  a
  2138.              goto  with  an  intervening core dump and reincarna-
  2139.              tion.  If LABEL is  omitted,  restarts  the  program
  2140.              from the top.  WARNING: any files opened at the time
  2141.              of the dump will NOT be open any more when the  pro-
  2142.              gram is reincarnated, with possible resulting confu-
  2143.              sion on the part of perl.  See also -u.
  2144.  
  2145.              Example:
  2146.  
  2147.                   #!/usr/bin/perl
  2148.                   require 'getopt.pl';
  2149.                   require 'stat.pl';
  2150.                   %days = (
  2151.                       'Sun',1,
  2152.                       'Mon',2,
  2153.                       'Tue',3,
  2154.                       'Wed',4,
  2155.                       'Thu',5,
  2156.                       'Fri',6,
  2157.                       'Sat',7);
  2158.  
  2159.                   dump QUICKSTART if $ARGV[0] eq '-d';
  2160.  
  2161.                  QUICKSTART:
  2162.                   do Getopt('f');
  2163.  
  2164.  
  2165.      each(ASSOC_ARRAY)
  2166.  
  2167.      each ASSOC_ARRAY
  2168.              Returns a 2 element array consisting of the key  and
  2169.              value for the next value of an associative array, so
  2170.              that you can iterate over it.  Entries are  returned
  2171.              in  an  apparently  random order.  When the array is
  2172.  
  2173.  
  2174.  
  2175. Sun Release 4.1           Last change:                         33
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182. PERL(1)                  USER COMMANDS                    PERL(1)
  2183.  
  2184.  
  2185.  
  2186.              entirely read, a null array is returned (which  when
  2187.              assigned produces a FALSE (0) value).  The next call
  2188.              to each() after that  will  start  iterating  again.
  2189.              The  iterator  can  be reset only by reading all the
  2190.              elements from the array.  You must  not  modify  the
  2191.              array  while  iterating  over it.  There is a single
  2192.              iterator for each associative array, shared  by  all
  2193.              each(),  keys()  and  values() function calls in the
  2194.              program.  The following prints out your  environment
  2195.              like  the  printenv  program,  only  in  a different
  2196.              order:
  2197.  
  2198.                   while (($key,$value) = each %ENV) {
  2199.                        print "$key=$value\n";
  2200.                   }
  2201.  
  2202.              See also keys() and values().
  2203.  
  2204.      eof(FILEHANDLE)
  2205.  
  2206.      eof()
  2207.  
  2208.      eof     Returns 1 if the next read on FILEHANDLE will return
  2209.              end of file, or if FILEHANDLE is not open.  FILEHAN-
  2210.              DLE may be an expression whose value gives the  real
  2211.              filehandle  name.  (Note that this function actually
  2212.              reads a character and then ungetc's it, so it is not
  2213.              very  useful  in  an  interactive  context.)  An eof
  2214.              without an argument returns the eof status  for  the
  2215.              last file read.  Empty parentheses () may be used to
  2216.              indicate the pseudo file formed of the files  listed
  2217.              on the command line, i.e. eof() is reasonable to use
  2218.              inside a while (<>) loop to detect the end  of  only
  2219.              the  last  file.   Use  eof(ARGV) or eof without the
  2220.              parentheses to test EACH file in a while (<>)  loop.
  2221.              Examples:
  2222.  
  2223.                   # insert dashes just before last line of last file
  2224.                   while (<>) {
  2225.                        if (eof()) {
  2226.                             print "--------------\n";
  2227.                        }
  2228.                        print;
  2229.                   }
  2230.  
  2231.                   # reset line numbering on each input file
  2232.                   while (<>) {
  2233.                        print "$.\t$_";
  2234.                        if (eof) {     # Not eof().
  2235.                             close(ARGV);
  2236.                        }
  2237.                   }
  2238.  
  2239.  
  2240.  
  2241. Sun Release 4.1           Last change:                         34
  2242.  
  2243.  
  2244.  
  2245.  
  2246.  
  2247.  
  2248. PERL(1)                  USER COMMANDS                    PERL(1)
  2249.  
  2250.  
  2251.  
  2252.      eval(EXPR)
  2253.  
  2254.      eval EXPR
  2255.              EXPR is parsed and executed as if it were  a  little
  2256.              perl  program.  It is executed in the context of the
  2257.              current perl program, so that any variable settings,
  2258.              subroutine  or format definitions remain afterwards.
  2259.              The value returned is the value of the last  expres-
  2260.              sion  evaluated, just as with subroutines.  If there
  2261.              is a syntax error or runtime error, or a die  state-
  2262.              ment  is executed, an undefined value is returned by
  2263.              eval, and $@ is set to the error message.  If  there
  2264.              was  no error, $@ is guaranteed to be a null string.
  2265.              If EXPR is omitted, evaluates $_.  The  final  semi-
  2266.              colon, if any, may be omitted from the expression.
  2267.  
  2268.              Note that, since eval traps otherwise-fatal  errors,
  2269.              it  is  useful  for determining whether a particular
  2270.              feature (such as dbmopen or symlink) is implemented.
  2271.              It  is  also  Perl's  exception  trapping mechanism,
  2272.              where the die operator is used to raise exceptions.
  2273.  
  2274.      exec(LIST)
  2275.  
  2276.      exec LIST
  2277.              If there is more than one argument in  LIST,  or  if
  2278.              LIST  is  an  array  with more than one value, calls
  2279.              execvp() with the arguments in LIST.   If  there  is
  2280.              only  one  scalar  argument, the argument is checked
  2281.              for shell metacharacters.  If  there  are  any,  the
  2282.              entire  argument is passed to "/bin/sh -c" for pars-
  2283.              ing.  If there are none, the argument is split  into
  2284.              words and passed directly to execvp(), which is more
  2285.              efficient.  Note: exec (and  system)  do  not  flush
  2286.              your  output  buffer,  so  you may need to set $| to
  2287.              avoid lost output.  Examples:
  2288.  
  2289.                   exec '/bin/echo', 'Your arguments are: ', @ARGV;
  2290.                   exec "sort $outfile | uniq";
  2291.  
  2292.  
  2293.              If you don't really want to execute the first  argu-
  2294.              ment, but want to lie to the program you are execut-
  2295.              ing about its own name, you can specify the  program
  2296.              you  actually  want  to  run  by assigning that to a
  2297.              variable and putting the name  of  the  variable  in
  2298.              front  of  the  LIST  without a comma.  (This always
  2299.              forces interpretation of the LIST as a  multi-valued
  2300.              list,  even  if there is only a single scalar in the
  2301.              list.) Example:
  2302.  
  2303.  
  2304.  
  2305.  
  2306.  
  2307. Sun Release 4.1           Last change:                         35
  2308.  
  2309.  
  2310.  
  2311.  
  2312.  
  2313.  
  2314. PERL(1)                  USER COMMANDS                    PERL(1)
  2315.  
  2316.  
  2317.  
  2318.                   $shell = '/bin/csh';
  2319.                   exec $shell '-sh';       # pretend it's a login shell
  2320.  
  2321.  
  2322.      exit(EXPR)
  2323.  
  2324.      exit EXPR
  2325.              Evaluates  EXPR  and  exits  immediately  with  that
  2326.              value.  Example:
  2327.  
  2328.                   $ans = <STDIN>;
  2329.                   exit 0 if $ans =~ /^[Xx]/;
  2330.  
  2331.              See also die.  If EXPR  is  omitted,  exits  with  0
  2332.              status.
  2333.  
  2334.      exp(EXPR)
  2335.  
  2336.      exp EXPR
  2337.              Returns e to the power of EXPR.  If EXPR is omitted,
  2338.              gives exp($_).
  2339.  
  2340.      fcntl(FILEHANDLE,FUNCTION,SCALAR)
  2341.              Implements the fcntl(2) function.   You'll  probably
  2342.              have to say
  2343.  
  2344.                   require "fcntl.ph"; # probably /usr/local/lib/perl/fcntl.ph
  2345.  
  2346.              first to get the correct function  definitions.   If
  2347.              fcntl.ph  doesn't  exist or doesn't have the correct
  2348.              definitions you'll have to roll your own,  based  on
  2349.              your  C  header files such as <sys/fcntl.h>.  (There
  2350.              is a perl script called h2ph  that  comes  with  the
  2351.              perl  kit which may help you in this.) Argument pro-
  2352.              cessing and  value  return  works  just  like  ioctl
  2353.              below.   Note  that fcntl will produce a fatal error
  2354.              if  used  on  a  machine  that   doesn't   implement
  2355.              fcntl(2).
  2356.  
  2357.      fileno(FILEHANDLE)
  2358.  
  2359.      fileno FILEHANDLE
  2360.              Returns the file descriptor for a filehandle.   Use-
  2361.              ful  for  constructing  bitmaps  for  select().   If
  2362.              FILEHANDLE is an expression, the value is  taken  as
  2363.              the name of the filehandle.
  2364.  
  2365.      flock(FILEHANDLE,OPERATION)
  2366.              Calls flock(2) on FILEHANDLE.  See manual  page  for
  2367.              flock(2)  for definition of OPERATION.  Returns true
  2368.              for success, false on failure.  Will produce a fatal
  2369.              error  if  used  on a machine that doesn't implement
  2370.  
  2371.  
  2372.  
  2373. Sun Release 4.1           Last change:                         36
  2374.  
  2375.  
  2376.  
  2377.  
  2378.  
  2379.  
  2380. PERL(1)                  USER COMMANDS                    PERL(1)
  2381.  
  2382.  
  2383.  
  2384.              flock(2).  Here's a mailbox appender  for  BSD  sys-
  2385.              tems.
  2386.  
  2387.                   $LOCK_SH = 1;
  2388.                   $LOCK_EX = 2;
  2389.                   $LOCK_NB = 4;
  2390.                   $LOCK_UN = 8;
  2391.  
  2392.                   sub lock {
  2393.                       flock(MBOX,$LOCK_EX);
  2394.                       # and, in case someone appended
  2395.                       # while we were waiting...
  2396.                       seek(MBOX, 0, 2);
  2397.                   }
  2398.  
  2399.                   sub unlock {
  2400.                       flock(MBOX,$LOCK_UN);
  2401.                   }
  2402.  
  2403.                   open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
  2404.                        || die "Can't open mailbox: $!";
  2405.  
  2406.                   do lock();
  2407.                   print MBOX $msg,"\n\n";
  2408.                   do unlock();
  2409.  
  2410.  
  2411.      fork    Does a fork() call.  Returns the child  pid  to  the
  2412.              parent  process  and  0 to the child process.  Note:
  2413.              unflushed   buffers   remain   unflushed   in   both
  2414.              processes,  which  means  you  may need to set $| to
  2415.              avoid duplicate output.
  2416.  
  2417.      getc(FILEHANDLE)
  2418.  
  2419.      getc FILEHANDLE
  2420.  
  2421.      getc    Returns the  next  character  from  the  input  file
  2422.              attached to FILEHANDLE, or a null string at EOF.  If
  2423.              FILEHANDLE is omitted, reads from STDIN.
  2424.  
  2425.      getlogin
  2426.              Returns the current login from  /etc/utmp,  if  any.
  2427.              If null, use getpwuid.
  2428.  
  2429.                   $login  =  getlogin  ||  (getpwuid($<))[0]   ||
  2430.              "Somebody";
  2431.  
  2432.  
  2433.      getpeername(SOCKET)
  2434.              Returns the packed sockaddr address of other end  of
  2435.              the SOCKET connection.
  2436.  
  2437.  
  2438.  
  2439. Sun Release 4.1           Last change:                         37
  2440.  
  2441.  
  2442.  
  2443.  
  2444.  
  2445.  
  2446. PERL(1)                  USER COMMANDS                    PERL(1)
  2447.  
  2448.  
  2449.  
  2450.                   # An internet sockaddr
  2451.                   $sockaddr = 'S n a4 x8';
  2452.                   $hersockaddr = getpeername(S);
  2453.                   ($family, $port, $heraddr) =
  2454.                             unpack($sockaddr,$hersockaddr);
  2455.  
  2456.  
  2457.      getpgrp(PID)
  2458.  
  2459.      getpgrp PID
  2460.              Returns the current process group for the  specified
  2461.              PID,  0  for  the  current  process.  Will produce a
  2462.              fatal error if used on a machine that doesn't imple-
  2463.              ment  getpgrp(2).   If EXPR is omitted, returns pro-
  2464.              cess group of current process.
  2465.  
  2466.      getppid Returns the process id of the parent process.
  2467.  
  2468.      getpriority(WHICH,WHO)
  2469.              Returns the current priority for a process,  a  pro-
  2470.              cess  group,  or a user.  (See getpriority(2).) Will
  2471.              produce a fatal error if  used  on  a  machine  that
  2472.              doesn't implement getpriority(2).
  2473.  
  2474.      getpwnam(NAME)
  2475.  
  2476.      getgrnam(NAME)
  2477.  
  2478.      gethostbyname(NAME)
  2479.  
  2480.      getnetbyname(NAME)
  2481.  
  2482.      getprotobyname(NAME)
  2483.  
  2484.      getpwuid(UID)
  2485.  
  2486.      getgrgid(GID)
  2487.  
  2488.      getservbyname(NAME,PROTO)
  2489.  
  2490.      gethostbyaddr(ADDR,ADDRTYPE)
  2491.  
  2492.      getnetbyaddr(ADDR,ADDRTYPE)
  2493.  
  2494.      getprotobynumber(NUMBER)
  2495.  
  2496.      getservbyport(PORT,PROTO)
  2497.  
  2498.      getpwent
  2499.  
  2500.      getgrent
  2501.  
  2502.  
  2503.  
  2504.  
  2505. Sun Release 4.1           Last change:                         38
  2506.  
  2507.  
  2508.  
  2509.  
  2510.  
  2511.  
  2512. PERL(1)                  USER COMMANDS                    PERL(1)
  2513.  
  2514.  
  2515.  
  2516.      gethostent
  2517.  
  2518.      getnetent
  2519.  
  2520.      getprotoent
  2521.  
  2522.      getservent
  2523.  
  2524.      setpwent
  2525.  
  2526.      setgrent
  2527.  
  2528.      sethostent(STAYOPEN)
  2529.  
  2530.      setnetent(STAYOPEN)
  2531.  
  2532.      setprotoent(STAYOPEN)
  2533.  
  2534.      setservent(STAYOPEN)
  2535.  
  2536.      endpwent
  2537.  
  2538.      endgrent
  2539.  
  2540.      endhostent
  2541.  
  2542.      endnetent
  2543.  
  2544.      endprotoent
  2545.  
  2546.      endservent
  2547.              These routines perform the same functions  as  their
  2548.              counterparts  in  the  system  library.   The return
  2549.              values from the various get routines are as follows:
  2550.  
  2551.                   ($name,$passwd,$uid,$gid,
  2552.                      $quota,$comment,$gcos,$dir,$shell) = getpw...
  2553.                   ($name,$passwd,$gid,$members) = getgr...
  2554.                   ($name,$aliases,$addrtype,$length,@addrs) = gethost...
  2555.                   ($name,$aliases,$addrtype,$net) = getnet...
  2556.                   ($name,$aliases,$proto) = getproto...
  2557.                   ($name,$aliases,$port,$proto) = getserv...
  2558.  
  2559.              The $members value returned by getgr... is  a  space
  2560.              separated  list of the login names of the members of
  2561.              the group.
  2562.  
  2563.              The @addrs value returned by  the  gethost...  func-
  2564.              tions is a list of the raw addresses returned by the
  2565.              corresponding system library call.  In the  Internet
  2566.              domain,  each address is four bytes long and you can
  2567.              unpack it by saying something like:
  2568.  
  2569.  
  2570.  
  2571. Sun Release 4.1           Last change:                         39
  2572.  
  2573.  
  2574.  
  2575.  
  2576.  
  2577.  
  2578. PERL(1)                  USER COMMANDS                    PERL(1)
  2579.  
  2580.  
  2581.  
  2582.                   ($a,$b,$c,$d) = unpack('C4',$addr[0]);
  2583.  
  2584.  
  2585.      getsockname(SOCKET)
  2586.              Returns the packed sockaddr address of this  end  of
  2587.              the SOCKET connection.
  2588.  
  2589.                   # An internet sockaddr
  2590.                   $sockaddr = 'S n a4 x8';
  2591.                   $mysockaddr = getsockname(S);
  2592.                   ($family, $port, $myaddr) =
  2593.                             unpack($sockaddr,$mysockaddr);
  2594.  
  2595.  
  2596.      getsockopt(SOCKET,LEVEL,OPTNAME)
  2597.              Returns the socket option requested, or undefined if
  2598.              there is an error.
  2599.  
  2600.      gmtime(EXPR)
  2601.  
  2602.      gmtime EXPR
  2603.              Converts a time as returned by the time function  to
  2604.              a  9-element  array  with  the time analyzed for the
  2605.              Greenwich timezone.  Typically used as follows:
  2606.  
  2607.              ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  2608.                                            gmtime(time);
  2609.  
  2610.              All array elements are numeric,  and  come  straight
  2611.              out  of  a struct tm.  In particular this means that
  2612.              $mon has the range 0..11 and  $wday  has  the  range
  2613.              0..6.  If EXPR is omitted, does gmtime(time).
  2614.  
  2615.      goto LABEL
  2616.              Finds the statement labeled with LABEL  and  resumes
  2617.              execution  there.   Currently  you  may  only  go to
  2618.              statements in the main body of the program that  are
  2619.              not nested inside a do {} construct.  This statement
  2620.              is not implemented very  efficiently,  and  is  here
  2621.              only  to  make the sed-to-perl translator easier.  I
  2622.              may change its semantics  at  any  time,  consistent
  2623.              with  support for translated sed scripts.  Use it at
  2624.              your own risk.  Better yet, don't use it at all.
  2625.  
  2626.      grep(EXPR,LIST)
  2627.              Evaluates EXPR for each  element  of  LIST  (locally
  2628.              setting  $_  to  each element) and returns the array
  2629.              value consisting of those  elements  for  which  the
  2630.              expression  evaluated to true.  In a scalar context,
  2631.              returns the number of times the expression was true.
  2632.  
  2633.                   @foo = grep(!/^#/, @bar);    # weed out comments
  2634.  
  2635.  
  2636.  
  2637. Sun Release 4.1           Last change:                         40
  2638.  
  2639.  
  2640.  
  2641.  
  2642.  
  2643.  
  2644. PERL(1)                  USER COMMANDS                    PERL(1)
  2645.  
  2646.  
  2647.  
  2648.              Note that, since $_ is a reference  into  the  array
  2649.              value,  it can be used to modify the elements of the
  2650.              array.  While this is useful and supported,  it  can
  2651.              cause  bizarre  results  if  the LIST is not a named
  2652.              array.
  2653.  
  2654.      hex(EXPR)
  2655.  
  2656.      hex EXPR
  2657.              Returns the decimal value of EXPR interpreted as  an
  2658.              hex  string.  (To interpret strings that might start
  2659.              with 0 or 0x see oct().) If EXPR  is  omitted,  uses
  2660.              $_.
  2661.  
  2662.      index(STR,SUBSTR,POSITION)
  2663.  
  2664.      index(STR,SUBSTR)
  2665.              Returns the position  of  the  first  occurrence  of
  2666.              SUBSTR  in STR at or after POSITION.  If POSITION is
  2667.              omitted, starts searching from the beginning of  the
  2668.              string.  The return value is based at 0, or whatever
  2669.              you've set the $[ variable to.  If the substring  is
  2670.              not  found,  returns  one  less than the base, ordi-
  2671.              narily -1.
  2672.  
  2673.      int(EXPR)
  2674.  
  2675.      int EXPR
  2676.              Returns the integer portion of  EXPR.   If  EXPR  is
  2677.              omitted, uses $_.
  2678.  
  2679.      ioctl(FILEHANDLE,FUNCTION,SCALAR)
  2680.              Implements the ioctl(2) function.   You'll  probably
  2681.              have to say
  2682.  
  2683.                   require "ioctl.ph"; # probably /usr/local/lib/perl/ioctl.ph
  2684.  
  2685.              first to get the correct function  definitions.   If
  2686.              ioctl.ph  doesn't  exist or doesn't have the correct
  2687.              definitions you'll have to roll your own,  based  on
  2688.              your  C  header files such as <sys/ioctl.h>.  (There
  2689.              is a perl script called h2ph  that  comes  with  the
  2690.              perl kit which may help you in this.) SCALAR will be
  2691.              read and/or written  depending  on  the  FUNCTION--a
  2692.              pointer to the string value of SCALAR will be passed
  2693.              as the third argument of the actual ioctl call.  (If
  2694.              SCALAR  has  no string value but does have a numeric
  2695.              value, that value  will  be  passed  rather  than  a
  2696.              pointer  to  the string value.  To guarantee this to
  2697.              be true, add a 0 to the scalar before using it.) The
  2698.              pack() and unpack() functions are useful for manipu-
  2699.              lating the values of  structures  used  by  ioctl().
  2700.  
  2701.  
  2702.  
  2703. Sun Release 4.1           Last change:                         41
  2704.  
  2705.  
  2706.  
  2707.  
  2708.  
  2709.  
  2710. PERL(1)                  USER COMMANDS                    PERL(1)
  2711.  
  2712.  
  2713.  
  2714.              The  following  example  sets the erase character to
  2715.              DEL.
  2716.  
  2717.                   require 'ioctl.ph';
  2718.                   $sgttyb_t = "ccccs";          # 4 chars and a short
  2719.                   if (ioctl(STDIN,$TIOCGETP,$sgttyb)) {
  2720.                        @ary = unpack($sgttyb_t,$sgttyb);
  2721.                        $ary[2] = 127;
  2722.                        $sgttyb = pack($sgttyb_t,@ary);
  2723.                        ioctl(STDIN,$TIOCSETP,$sgttyb)
  2724.                             || die "Can't ioctl: $!";
  2725.                   }
  2726.  
  2727.              The return value of ioctl (and fcntl) is as follows:
  2728.  
  2729.                   if OS returns:           perl returns:
  2730.                     -1                       undefined value
  2731.                     0                        string "0 but true"
  2732.                     anything else            that number
  2733.  
  2734.              Thus perl returns  true  on  success  and  false  on
  2735.              failure,  yet  you  can  still  easily determine the
  2736.              actual value returned by the operating system:
  2737.  
  2738.                   ($retval = ioctl(...)) || ($retval = -1);
  2739.                   printf "System returned %d\n", $retval;
  2740.  
  2741.      join(EXPR,LIST)
  2742.  
  2743.      join(EXPR,ARRAY)
  2744.              Joins the separate strings of LIST or ARRAY  into  a
  2745.              single  string with fields separated by the value of
  2746.              EXPR, and returns the string.  Example:
  2747.  
  2748.              $_ = join(':',
  2749.                        $login,$passwd,$uid,$gid,$gcos,$home,$shell);
  2750.  
  2751.              See split.
  2752.  
  2753.      keys(ASSOC_ARRAY)
  2754.  
  2755.      keys ASSOC_ARRAY
  2756.              Returns a normal array consisting of all the keys of
  2757.              the  named associative array.  The keys are returned
  2758.              in an apparently random order, but it  is  the  same
  2759.              order as either the values() or each() function pro-
  2760.              duces (given that the associative array has not been
  2761.              modified).   Here  is  yet another way to print your
  2762.              environment:
  2763.  
  2764.  
  2765.  
  2766.  
  2767.  
  2768.  
  2769. Sun Release 4.1           Last change:                         42
  2770.  
  2771.  
  2772.  
  2773.  
  2774.  
  2775.  
  2776. PERL(1)                  USER COMMANDS                    PERL(1)
  2777.  
  2778.  
  2779.  
  2780.                   @keys = keys %ENV;
  2781.                   @values = values %ENV;
  2782.                   while ($#keys >= 0) {
  2783.                        print pop(@keys), '=', pop(@values), "\n";
  2784.                   }
  2785.  
  2786.              or how about sorted by key:
  2787.  
  2788.                   foreach $key (sort(keys %ENV)) {
  2789.                        print $key, '=', $ENV{$key}, "\n";
  2790.                   }
  2791.  
  2792.  
  2793.      kill(LIST)
  2794.  
  2795.      kill LIST
  2796.              Sends a signal to a list of  processes.   The  first
  2797.              element  of  the  list  must  be the signal to send.
  2798.              Returns the number of  processes  successfully  sig-
  2799.              naled.
  2800.  
  2801.                   $cnt = kill 1, $child1, $child2;
  2802.                   kill 9, @goners;
  2803.  
  2804.              If the signal  is  negative,  kills  process  groups
  2805.              instead of processes.  (On System V, a negative pro-
  2806.              cess number  will  also  kill  process  groups,  but
  2807.              that's  not  portable.) You may use a signal name in
  2808.              quotes.
  2809.  
  2810.      last LABEL
  2811.  
  2812.      last    The last command is like the break  statement  in  C
  2813.              (as used in loops); it immediately exits the loop in
  2814.              question.  If the  LABEL  is  omitted,  the  command
  2815.              refers  to  the  innermost enclosing loop.  The con-
  2816.              tinue block, if any, is not executed:
  2817.  
  2818.                   line: while (<STDIN>) {
  2819.                        last line if /^$/;  # exit when done with header
  2820.                        ...
  2821.                   }
  2822.  
  2823.  
  2824.      length(EXPR)
  2825.  
  2826.      length EXPR
  2827.              Returns the length in characters  of  the  value  of
  2828.              EXPR.  If EXPR is omitted, returns length of $_.
  2829.  
  2830.      link(OLDFILE,NEWFILE)
  2831.              Creates a new filename linked to the  old  filename.
  2832.  
  2833.  
  2834.  
  2835. Sun Release 4.1           Last change:                         43
  2836.  
  2837.  
  2838.  
  2839.  
  2840.  
  2841.  
  2842. PERL(1)                  USER COMMANDS                    PERL(1)
  2843.  
  2844.  
  2845.  
  2846.              Returns 1 for success, 0 otherwise.
  2847.  
  2848.      listen(SOCKET,QUEUESIZE)
  2849.              Does the same thing  that  the  listen  system  call
  2850.              does.   Returns  true  if it succeeded, false other-
  2851.              wise.  See example in section on  Interprocess  Com-
  2852.              munication.
  2853.  
  2854.      local(LIST)
  2855.              Declares the listed variables to  be  local  to  the
  2856.              enclosing  block, subroutine, eval or "do".  All the
  2857.              listed elements must be legal lvalues.  This  opera-
  2858.              tor  works  by  saving  the  current values of those
  2859.              variables in LIST on a hidden  stack  and  restoring
  2860.              them  upon  exiting  the  block, subroutine or eval.
  2861.              This means that called subroutines can  also  refer-
  2862.              ence  the  local  variable,  but not the global one.
  2863.              The LIST may be assigned to if desired, which allows
  2864.              you to initialize your local variables.  (If no ini-
  2865.              tializer is given for a particular variable,  it  is
  2866.              created  with  an undefined value.) Commonly this is
  2867.              used to name the parameters to a subroutine.   Exam-
  2868.              ples:
  2869.  
  2870.                   sub RANGEVAL {
  2871.                        local($min, $max, $thunk) = @_;
  2872.                        local($result) = '';
  2873.                        local($i);
  2874.  
  2875.                        # Presumably $thunk makes reference to $i
  2876.  
  2877.                        for ($i = $min; $i < $max; $i++) {
  2878.                             $result .= eval $thunk;
  2879.                        }
  2880.  
  2881.                        $result;
  2882.                   }
  2883.  
  2884.                   if ($sw eq '-v') {
  2885.                       # init local array with global array
  2886.                       local(@ARGV) = @ARGV;
  2887.                       unshift(@ARGV,'echo');
  2888.                       system @ARGV;
  2889.                   }
  2890.                   # @ARGV restored
  2891.  
  2892.                   # temporarily add to digits associative array
  2893.                   if ($base12) {
  2894.                        # (NOTE: not claiming this is efficient!)
  2895.                        local(%digits) = (%digits,'t',10,'e',11);
  2896.                        do parse_num();
  2897.                   }
  2898.  
  2899.  
  2900.  
  2901. Sun Release 4.1           Last change:                         44
  2902.  
  2903.  
  2904.  
  2905.  
  2906.  
  2907.  
  2908. PERL(1)                  USER COMMANDS                    PERL(1)
  2909.  
  2910.  
  2911.  
  2912.              Note that local() is a run-time command, and so gets
  2913.              executed  every  time  through a loop, using up more
  2914.              stack storage each time until it's all  released  at
  2915.              once when the loop is exited.
  2916.  
  2917.      localtime(EXPR)
  2918.  
  2919.      localtime EXPR
  2920.              Converts a time as returned by the time function  to
  2921.              a  9-element  array  with  the time analyzed for the
  2922.              local timezone.  Typically used as follows:
  2923.  
  2924.              ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
  2925.                                            localtime(time);
  2926.  
  2927.              All array elements are numeric,  and  come  straight
  2928.              out  of  a struct tm.  In particular this means that
  2929.              $mon has the range 0..11 and  $wday  has  the  range
  2930.              0..6.  If EXPR is omitted, does localtime(time).
  2931.  
  2932.      log(EXPR)
  2933.  
  2934.      log EXPR
  2935.              Returns logarithm (base e)  of  EXPR.   If  EXPR  is
  2936.              omitted, returns log of $_.
  2937.  
  2938.      lstat(FILEHANDLE)
  2939.  
  2940.      lstat FILEHANDLE
  2941.  
  2942.      lstat(EXPR)
  2943.  
  2944.      lstat SCALARVARIABLE
  2945.              Does the same thing  as  the  stat()  function,  but
  2946.              stats  a  symbolic link instead of the file the sym-
  2947.              bolic link points to.  If symbolic links  are  unim-
  2948.              plemented on your system, a normal stat is done.
  2949.  
  2950.      m/PATTERN/io
  2951.  
  2952.      /PATTERN/io
  2953.              Searches a string for a pattern match,  and  returns
  2954.              true  (1)  or false ('').  If no string is specified
  2955.              via  the  =~  or  !~  operator,  the  $_  string  is
  2956.              searched.  (The string specified with =~ need not be
  2957.              an lvalue--it may be the  result  of  an  expression
  2958.              evaluation,   but   remember  the  =~  binds  rather
  2959.              tightly.) See also the section  on  regular  expres-
  2960.              sions.
  2961.  
  2962.              If / is  the  delimiter  then  the  initial  'm'  is
  2963.              optional.   With  the  'm'  you  can use any pair of
  2964.  
  2965.  
  2966.  
  2967. Sun Release 4.1           Last change:                         45
  2968.  
  2969.  
  2970.  
  2971.  
  2972.  
  2973.  
  2974. PERL(1)                  USER COMMANDS                    PERL(1)
  2975.  
  2976.  
  2977.  
  2978.              non-alphanumeric characters as delimiters.  This  is
  2979.              particularly  useful  for  matching  Unix path names
  2980.              that contain '/'.  If the final  delimiter  is  fol-
  2981.              lowed  by  the  optional letter 'i', the matching is
  2982.              done in a case-insensitive manner.  PATTERN may con-
  2983.              tain  references  to scalar variables, which will be
  2984.              interpolated (and the pattern recompiled) every time
  2985.              the  pattern search is evaluated.  (Note that $) and
  2986.              $| may not be interpolated because  they  look  like
  2987.              end-of-string  tests.) If you want such a pattern to
  2988.              be compiled only once, add an "o" after the trailing
  2989.              delimiter.   This avoids expensive run-time recompi-
  2990.              lations, and is useful when the value you are inter-
  2991.              polating won't change over the life of the script.
  2992.  
  2993.              If used in a context that requires an array value, a
  2994.              pattern  match  returns  an  array consisting of the
  2995.              subexpressions matched by  the  parentheses  in  the
  2996.              pattern, i.e. ($1, $2, $3...).  It does NOT actually
  2997.              set $1, $2, etc. in this case, nor does it  set  $+,
  2998.              $`,  $&  or $'.  If the match fails, a null array is
  2999.              returned.  If the match succeeds, but there were  no
  3000.              parentheses, an array value of (1) is returned.
  3001.  
  3002.              Examples:
  3003.  
  3004.                  open(tty, '/dev/tty');
  3005.                  <tty> =~ /^y/i && do foo();    # do foo if desired
  3006.  
  3007.                  if (/Version: *([0-9.]*)/) { $version = $1; }
  3008.  
  3009.                  next if m#^/usr/spool/uucp#;
  3010.  
  3011.                  # poor man's grep
  3012.                  $arg = shift;
  3013.                  while (<>) {
  3014.                       print if /$arg/o;    # compile only once
  3015.                  }
  3016.  
  3017.                  if (($F1, $F2, $Etc) = ($foo =~ /^(\S+)\s+(\S+)\s*(.*)/))
  3018.  
  3019.              This last example splits $foo  into  the  first  two
  3020.              words  and  the  remainder  of the line, and assigns
  3021.              those three fields to $F1, $F2 and $Etc.  The condi-
  3022.              tional  is true if any variables were assigned, i.e.
  3023.              if the pattern matched.
  3024.  
  3025.      mkdir(FILENAME,MODE)
  3026.              Creates the directory specified  by  FILENAME,  with
  3027.              permissions   specified  by  MODE  (as  modified  by
  3028.              umask).  If it succeeds it returns 1,  otherwise  it
  3029.              returns 0 and sets $! (errno).
  3030.  
  3031.  
  3032.  
  3033. Sun Release 4.1           Last change:                         46
  3034.  
  3035.  
  3036.  
  3037.  
  3038.  
  3039.  
  3040. PERL(1)                  USER COMMANDS                    PERL(1)
  3041.  
  3042.  
  3043.  
  3044.      msgctl(ID,CMD,ARG)
  3045.              Calls the System V IPC function msgctl.  If  CMD  is
  3046.              &IPC_STAT,  then  ARG  must be a variable which will
  3047.              hold the returned msqid_ds structure.  Returns  like
  3048.              ioctl:  the  undefined value for error, "0 but true"
  3049.              for zero, or the actual return value otherwise.
  3050.  
  3051.      msgget(KEY,FLAGS)
  3052.              Calls the System V IPC function msgget.  Returns the
  3053.              message queue id, or the undefined value if there is
  3054.              an error.
  3055.  
  3056.      msgsnd(ID,MSG,FLAGS)
  3057.              Calls the System V IPC function msgsnd to  send  the
  3058.              message MSG to the message queue ID.  MSG must begin
  3059.              with the long integer message  type,  which  may  be
  3060.              created with pack("L", $type).  Returns true if suc-
  3061.              cessful, or false if there is an error.
  3062.  
  3063.      msgrcv(ID,VAR,SIZE,TYPE,FLAGS)
  3064.              Calls the System V IPC function msgrcv to receive  a
  3065.              message from message queue ID into variable VAR with
  3066.              a maximum message size of SIZE.  Note that if a mes-
  3067.              sage is received, the message type will be the first
  3068.              thing in VAR, and the maximum length of VAR is  SIZE
  3069.              plus  the size of the message type.  Returns true if
  3070.              successful, or false if there is an error.
  3071.  
  3072.  
  3073.      next LABEL
  3074.  
  3075.      next    The next command is like the continue  statement  in
  3076.              C; it starts the next iteration of the loop:
  3077.  
  3078.                   line: while (<STDIN>) {
  3079.                        next line if /^#/;  # discard comments
  3080.                        ...
  3081.                   }
  3082.  
  3083.              Note that if there were  a  continue  block  on  the
  3084.              above,  it  would  get  executed  even  on discarded
  3085.              lines.  If the LABEL is omitted, the command  refers
  3086.              to the innermost enclosing loop.
  3087.  
  3088.      oct(EXPR)
  3089.  
  3090.      oct EXPR
  3091.              Returns the decimal value of EXPR interpreted as  an
  3092.              octal  string.   (If  EXPR happens to start off with
  3093.              0x, interprets it as a hex string instead.) The fol-
  3094.              lowing  will  handle  decimal,  octal and hex in the
  3095.              standard notation:
  3096.  
  3097.  
  3098.  
  3099. Sun Release 4.1           Last change:                         47
  3100.  
  3101.  
  3102.  
  3103.  
  3104.  
  3105.  
  3106. PERL(1)                  USER COMMANDS                    PERL(1)
  3107.  
  3108.  
  3109.  
  3110.                   $val = oct($val) if $val =~ /^0/;
  3111.  
  3112.              If EXPR is omitted, uses $_.
  3113.  
  3114.      open(FILEHANDLE,EXPR)
  3115.  
  3116.      open(FILEHANDLE)
  3117.  
  3118.      open FILEHANDLE
  3119.              Opens the file whose filename is given by EXPR,  and
  3120.              associates  it with FILEHANDLE.  If FILEHANDLE is an
  3121.              expression, its value is used as  the  name  of  the
  3122.              real  filehandle  wanted.   If  EXPR is omitted, the
  3123.              scalar variable of the same name as  the  FILEHANDLE
  3124.              contains  the filename.  If the filename begins with
  3125.              "<" or nothing, the file is opened  for  input.   If
  3126.              the filename begins with ">", the file is opened for
  3127.              output.  If the filename begins with ">>", the  file
  3128.              is  opened  for  appending.   (You  can put a '+' in
  3129.              front of the '>' or '<' to indicate  that  you  want
  3130.              both  read  and  write  access  to the file.) If the
  3131.              filename begins with "|",  the  filename  is  inter-
  3132.              preted  as a command to which output is to be piped,
  3133.              and if the filename ends with a "|", the filename is
  3134.              interpreted  as  command  which  pipes  input to us.
  3135.              (You may not have a command that pipes both  in  and
  3136.              out.) Opening '-' opens STDIN and opening '>-' opens
  3137.              STDOUT.  Open returns  non-zero  upon  success,  the
  3138.              undefined  value  otherwise.  If the open involved a
  3139.              pipe, the return value happens to be the pid of  the
  3140.              subprocess.  Examples:
  3141.  
  3142.                   $article = 100;
  3143.                   open article || die "Can't find article $article: $!\n";
  3144.                   while (<article>) {...
  3145.  
  3146.                   open(LOG, '>>/usr/spool/news/twitlog');
  3147.                                       # (log is reserved)
  3148.  
  3149.                   open(article, "caesar <$article |");
  3150.                                       # decrypt article
  3151.  
  3152.                   open(extract, "|sort >/tmp/Tmp$$");
  3153.                                       # $$ is our process#
  3154.  
  3155.                   # process argument list of files along with any includes
  3156.  
  3157.                   foreach $file (@ARGV) {
  3158.                        do process($file, 'fh00');    # no pun intended
  3159.                   }
  3160.  
  3161.                   sub process {
  3162.  
  3163.  
  3164.  
  3165. Sun Release 4.1           Last change:                         48
  3166.  
  3167.  
  3168.  
  3169.  
  3170.  
  3171.  
  3172. PERL(1)                  USER COMMANDS                    PERL(1)
  3173.  
  3174.  
  3175.  
  3176.                        local($filename, $input) = @_;
  3177.                        $input++;      # this is a string increment
  3178.                        unless (open($input, $filename)) {
  3179.                             print STDERR "Can't open $filename: $!\n";
  3180.                             return;
  3181.                        }
  3182.                        while (<$input>) {       # note use of indirection
  3183.                             if (/^#include "(.*)"/) {
  3184.                                  do process($1, $input);
  3185.                                  next;
  3186.                             }
  3187.                             ...       # whatever
  3188.                        }
  3189.                   }
  3190.  
  3191.              You may also, in the Bourne shell tradition, specify
  3192.              an  EXPR beginning with ">&", in which case the rest
  3193.              of the string  is  interpreted  as  the  name  of  a
  3194.              filehandle (or file descriptor, if numeric) which is
  3195.              to be duped and opened.  You may use & after >,  >>,
  3196.              <,  +>,  +>>  and  +<.   The mode you specify should
  3197.              match the mode of the original filehandle.  Here  is
  3198.              a  script that saves, redirects, and restores STDOUT
  3199.              and STDERR:
  3200.  
  3201.                   #!/usr/bin/perl
  3202.                   open(SAVEOUT, ">&STDOUT");
  3203.                   open(SAVEERR, ">&STDERR");
  3204.  
  3205.                   open(STDOUT, ">foo.out") || die "Can't redirect stdout";
  3206.                   open(STDERR, ">&STDOUT") || die "Can't dup stdout";
  3207.  
  3208.                   select(STDERR); $| = 1;       # make unbuffered
  3209.                   select(STDOUT); $| = 1;       # make unbuffered
  3210.  
  3211.                   print STDOUT "stdout 1\n";    # this works for
  3212.                   print STDERR "stderr 1\n";    # subprocesses too
  3213.  
  3214.                   close(STDOUT);
  3215.                   close(STDERR);
  3216.  
  3217.                   open(STDOUT, ">&SAVEOUT");
  3218.                   open(STDERR, ">&SAVEERR");
  3219.  
  3220.                   print STDOUT "stdout 2\n";
  3221.                   print STDERR "stderr 2\n";
  3222.  
  3223.              If you open a pipe on the command "-",  i.e.  either
  3224.              "|-"  or  "-|", then there is an implicit fork done,
  3225.              and the return value of open is the pid of the child
  3226.              within  the  parent  process, and 0 within the child
  3227.              process.  (Use defined($pid)  to  determine  if  the
  3228.  
  3229.  
  3230.  
  3231. Sun Release 4.1           Last change:                         49
  3232.  
  3233.  
  3234.  
  3235.  
  3236.  
  3237.  
  3238. PERL(1)                  USER COMMANDS                    PERL(1)
  3239.  
  3240.  
  3241.  
  3242.              open  was  successful.)  The filehandle behaves nor-
  3243.              mally for the parent, but i/o to that filehandle  is
  3244.              piped from/to the STDOUT/STDIN of the child process.
  3245.              In the child process the filehandle  isn't  opened--
  3246.              i/o  happens from/to the new STDOUT or STDIN.  Typi-
  3247.              cally this is used like the normal piped  open  when
  3248.              you  want to exercise more control over just how the
  3249.              pipe command gets executed, such  as  when  you  are
  3250.              running setuid, and don't want to have to scan shell
  3251.              commands for metacharacters.   The  following  pairs
  3252.              are more or less equivalent:
  3253.  
  3254.                   open(FOO, "|tr '[a-z]' '[A-Z]'");
  3255.                   open(FOO, "|-") || exec 'tr', '[a-z]', '[A-Z]';
  3256.  
  3257.                   open(FOO, "cat -n '$file'|");
  3258.                   open(FOO, "-|") || exec 'cat', '-n', $file;
  3259.  
  3260.              Explicitly closing any piped filehandle  causes  the
  3261.              parent  process to wait for the child to finish, and
  3262.              returns the status value in $?.  Note: on any opera-
  3263.              tion  which  may do a fork, unflushed buffers remain
  3264.              unflushed in both processes,  which  means  you  may
  3265.              need to set $| to avoid duplicate output.
  3266.  
  3267.              The filename that is passed to open will have  lead-
  3268.              ing  and  trailing  whitespace deleted.  In order to
  3269.              open a file with arbitrary weird characters  in  it,
  3270.              it's  necessary  to protect any leading and trailing
  3271.              whitespace thusly:
  3272.  
  3273.                      $file =~ s#^(\s)#./$1#;
  3274.                      open(FOO, "< $file\0");
  3275.  
  3276.  
  3277.      opendir(DIRHANDLE,EXPR)
  3278.              Opens a directory named EXPR for processing by read-
  3279.              dir(),   telldir(),   seekdir(),   rewinddir()   and
  3280.              closedir().  Returns true if successful.  DIRHANDLEs
  3281.              have their own namespace separate from FILEHANDLEs.
  3282.  
  3283.      ord(EXPR)
  3284.  
  3285.      ord EXPR
  3286.              Returns the numeric ascii value of the first charac-
  3287.              ter of EXPR.  If EXPR is omitted, uses $_.
  3288.  
  3289.      pack(TEMPLATE,LIST)
  3290.              Takes an array or list of values and packs it into a
  3291.              binary  structure,  returning  the string containing
  3292.              the structure.  The TEMPLATE is a sequence of  char-
  3293.              acters  that  give  the order and type of values, as
  3294.  
  3295.  
  3296.  
  3297. Sun Release 4.1           Last change:                         50
  3298.  
  3299.  
  3300.  
  3301.  
  3302.  
  3303.  
  3304. PERL(1)                  USER COMMANDS                    PERL(1)
  3305.  
  3306.  
  3307.  
  3308.              follows:
  3309.  
  3310.                   A    An ascii string, will be space padded.
  3311.                   a    An ascii string, will be null padded.
  3312.                   c    A signed char value.
  3313.                   C    An unsigned char value.
  3314.                   s    A signed short value.
  3315.                   S    An unsigned short value.
  3316.                   i    A signed integer value.
  3317.                   I    An unsigned integer value.
  3318.                   l    A signed long value.
  3319.                   L    An unsigned long value.
  3320.                   n    A short in "network" order.
  3321.                   N    A long in "network" order.
  3322.                   f    A single-precision float in the native format.
  3323.                   d    A double-precision float in the native format.
  3324.                   p    A pointer to a string.
  3325.                   x    A null byte.
  3326.                   X    Back up a byte.
  3327.                   @    Null fill to absolute position.
  3328.                   u    A uuencoded string.
  3329.                   b    A bit string (ascending bit order, like vec()).
  3330.                   B    A bit string (descending bit order).
  3331.                   h    A hex string (low nybble first).
  3332.                   H    A hex string (high nybble first).
  3333.  
  3334.              Each letter may optionally be followed by  a  number
  3335.              which  gives  a repeat count.  With all types except
  3336.              "a", "A", "b", "B", "h" and "H", the  pack  function
  3337.              will  gobble up that many values from the LIST.  A *
  3338.              for the repeat count means to use however many items
  3339.              are  left.   The  "a"  and "A" types gobble just one
  3340.              value, but pack it as a string of length count, pad-
  3341.              ding  with  nulls  or  spaces  as  necessary.  (When
  3342.              unpacking, "A" strips trailing spaces and nulls, but
  3343.              "a" does not.) Likewise, the "b" and "B" fields pack
  3344.              a string that many  bits  long.   The  "h"  and  "H"
  3345.              fields  pack  a string that many nybbles long.  Real
  3346.              numbers (floats  and  doubles)  are  in  the  native
  3347.              machine  format  only;  due  to  the multiplicity of
  3348.              floating formats around, and the lack of a  standard
  3349.              "network"  representation,  no  facility  for inter-
  3350.              change has been made.  This means that packed float-
  3351.              ing  point  data  written  on one machine may not be
  3352.              readable on another - even if both use IEEE floating
  3353.              point  arithmetic  (as the endian-ness of the memory
  3354.              representation is not part of the IEEE spec).   Note
  3355.              that  perl  uses  doubles internally for all numeric
  3356.              calculation, and converting from double -> float  ->
  3357.              double   will   lose   precision  (i.e.  unpack("f",
  3358.              pack("f", $foo)) will not in general equal $foo).
  3359.              Examples:
  3360.  
  3361.  
  3362.  
  3363. Sun Release 4.1           Last change:                         51
  3364.  
  3365.  
  3366.  
  3367.  
  3368.  
  3369.  
  3370. PERL(1)                  USER COMMANDS                    PERL(1)
  3371.  
  3372.  
  3373.  
  3374.                   $foo = pack("cccc",65,66,67,68);
  3375.                   # foo eq "ABCD"
  3376.                   $foo = pack("c4",65,66,67,68);
  3377.                   # same thing
  3378.  
  3379.                   $foo = pack("ccxxcc",65,66,67,68);
  3380.                   # foo eq "AB\0\0CD"
  3381.  
  3382.                   $foo = pack("s2",1,2);
  3383.                   # "\1\0\2\0" on little-endian
  3384.                   # "\0\1\0\2" on big-endian
  3385.  
  3386.                   $foo = pack("a4","abcd","x","y","z");
  3387.                   # "abcd"
  3388.  
  3389.                   $foo = pack("aaaa","abcd","x","y","z");
  3390.                   # "axyz"
  3391.  
  3392.                   $foo = pack("a14","abcdefg");
  3393.                   # "abcdefg\0\0\0\0\0\0\0"
  3394.  
  3395.                   $foo = pack("i9pl", gmtime);
  3396.                   # a real struct tm (on my system anyway)
  3397.  
  3398.                   sub bintodec {
  3399.                       unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
  3400.                   }
  3401.              The same template may generally also be used in  the
  3402.              unpack function.
  3403.  
  3404.      pipe(READHANDLE,WRITEHANDLE)
  3405.              Opens a pair of connected pipes like the correspond-
  3406.              ing  system call.  Note that if you set up a loop of
  3407.              piped processes, deadlock can occur unless  you  are
  3408.              very  careful.   In addition, note that perl's pipes
  3409.              use stdio buffering, so you may need to  set  $|  to
  3410.              flush your WRITEHANDLE after each command, depending
  3411.              on   the   application.    [Requires   version   3.0
  3412.              patchlevel 9.]
  3413.  
  3414.      pop(ARRAY)
  3415.  
  3416.      pop ARRAY
  3417.              Pops and returns the last value of the array,  shor-
  3418.              tening the array by 1.  Has the same effect as
  3419.  
  3420.                   $tmp = $ARRAY[$#ARRAY--];
  3421.  
  3422.              If there are no elements in the array,  returns  the
  3423.              undefined value.
  3424.  
  3425.  
  3426.  
  3427.  
  3428.  
  3429. Sun Release 4.1           Last change:                         52
  3430.  
  3431.  
  3432.  
  3433.  
  3434.  
  3435.  
  3436. PERL(1)                  USER COMMANDS                    PERL(1)
  3437.  
  3438.  
  3439.  
  3440.      print(FILEHANDLE LIST)
  3441.  
  3442.      print(LIST)
  3443.  
  3444.      print FILEHANDLE LIST
  3445.  
  3446.      print LIST
  3447.  
  3448.      print   Prints  a  string  or  a  comma-separated  list   of
  3449.              strings.   Returns non-zero if successful.  FILEHAN-
  3450.              DLE may be a scalar variable name, in which case the
  3451.              variable  contains  the name of the filehandle, thus
  3452.              introducing one level  of  indirection.   (NOTE:  If
  3453.              FILEHANDLE  is  a  variable  and the next token is a
  3454.              term, it may be misinterpreted as an operator unless
  3455.              you  interpose  a  +  or put parens around the argu-
  3456.              ments.) If FILEHANDLE is omitted, prints by  default
  3457.              to  standard  output (or to the last selected output
  3458.              channel--see select()).  If LIST  is  also  omitted,
  3459.              prints  $_  to  STDOUT.   To  set the default output
  3460.              channel to  something  other  than  STDOUT  use  the
  3461.              select  operation.  Note that, because print takes a
  3462.              LIST, anything in the LIST is evaluated in an  array
  3463.              context,  and any subroutine that you call will have
  3464.              one or more of its expressions evaluated in an array
  3465.              context.   Also  be  careful not to follow the print
  3466.              keyword with a left parenthesis unless you want  the
  3467.              corresponding  right  parenthesis  to  terminate the
  3468.              arguments to the print-interpose a + or  put  parens
  3469.              around all the arguments.
  3470.  
  3471.      printf(FILEHANDLE LIST)
  3472.  
  3473.      printf(LIST)
  3474.  
  3475.      printf FILEHANDLE LIST
  3476.  
  3477.      printf LIST
  3478.              Equivalent to a "print FILEHANDLE sprintf(LIST)".
  3479.  
  3480.      push(ARRAY,LIST)
  3481.              Treats ARRAY (@ is optional) as a stack, and  pushes
  3482.              the  values  of  LIST  onto  the  end of ARRAY.  The
  3483.              length of ARRAY increases by  the  length  of  LIST.
  3484.              Has the same effect as
  3485.  
  3486.                  for $value (LIST) {
  3487.                       $ARRAY[++$#ARRAY] = $value;
  3488.                  }
  3489.  
  3490.              but is more efficient.
  3491.  
  3492.  
  3493.  
  3494.  
  3495. Sun Release 4.1           Last change:                         53
  3496.  
  3497.  
  3498.  
  3499.  
  3500.  
  3501.  
  3502. PERL(1)                  USER COMMANDS                    PERL(1)
  3503.  
  3504.  
  3505.  
  3506.      q/STRING/
  3507.  
  3508.      qq/STRING/
  3509.              These are not really functions, but simply syntactic
  3510.              sugar  to let you avoid putting too many backslashes
  3511.              into quoted strings.  The q operator is  a  general-
  3512.              ized single quote, and the qq operator a generalized
  3513.              double quote.  Any non-alphanumeric delimiter can be
  3514.              used  in place of /, including newline.  If the del-
  3515.              imiter is an opening  bracket  or  parenthesis,  the
  3516.              final  delimiter  will  be the corresponding closing
  3517.              bracket or parenthesis.   (Embedded  occurrences  of
  3518.              the  closing  bracket  need  to  be  backslashed  as
  3519.              usual.) Examples:
  3520.  
  3521.                   $foo = q!I said, "You said, 'She said it.'"!;
  3522.                   $bar = q('This is it.');
  3523.                   $_ .= qq
  3524.              *** The previous line contains the naughty word "$&".\n
  3525.                        if /(ibm|apple|awk)/;      # :-)
  3526.  
  3527.  
  3528.      rand(EXPR)
  3529.  
  3530.      rand EXPR
  3531.  
  3532.      rand    Returns a random fractional number between 0 and the
  3533.              value  of  EXPR.  (EXPR should be positive.) If EXPR
  3534.              is omitted, returns a value between 0  and  1.   See
  3535.              also srand().
  3536.  
  3537.      read(FILEHANDLE,SCALAR,LENGTH,OFFSET)
  3538.  
  3539.      read(FILEHANDLE,SCALAR,LENGTH)
  3540.              Attempts to read LENGTH bytes of data into  variable
  3541.              SCALAR  from  the specified FILEHANDLE.  Returns the
  3542.              number of bytes actually read, or undef if there was
  3543.              an  error.   SCALAR  will  be grown or shrunk to the
  3544.              length actually read.  An OFFSET may be specified to
  3545.              place  the  read  data  at some other place than the
  3546.              beginning of the  string.   This  call  is  actually
  3547.              implemented  in terms of stdio's fread call.  To get
  3548.              a true read system call, see sysread.
  3549.  
  3550.      readdir(DIRHANDLE)
  3551.  
  3552.      readdir DIRHANDLE
  3553.              Returns the next directory  entry  for  a  directory
  3554.              opened  by  opendir().  If used in an array context,
  3555.              returns all the rest of the entries  in  the  direc-
  3556.              tory.   If  there  are  no  more entries, returns an
  3557.              undefined value in a scalar context or a  null  list
  3558.  
  3559.  
  3560.  
  3561. Sun Release 4.1           Last change:                         54
  3562.  
  3563.  
  3564.  
  3565.  
  3566.  
  3567.  
  3568. PERL(1)                  USER COMMANDS                    PERL(1)
  3569.  
  3570.  
  3571.  
  3572.              in an array context.
  3573.  
  3574.      readlink(EXPR)
  3575.  
  3576.      readlink EXPR
  3577.              Returns the value of a symbolic  link,  if  symbolic
  3578.              links are implemented.  If not, gives a fatal error.
  3579.              If there is some system error, returns the undefined
  3580.              value and sets $! (errno).  If EXPR is omitted, uses
  3581.              $_.
  3582.  
  3583.      recv(SOCKET,SCALAR,LEN,FLAGS)
  3584.              Receives a message on a socket.  Attempts to receive
  3585.              LENGTH  bytes  of data into variable SCALAR from the
  3586.              specified SOCKET filehandle.  Returns the address of
  3587.              the  sender,  or  the  undefined value if there's an
  3588.              error.  SCALAR will be grown or shrunk to the length
  3589.              actually  read.   Takes the same flags as the system
  3590.              call of the same name.
  3591.  
  3592.      redo LABEL
  3593.  
  3594.      redo    The redo command restarts  the  loop  block  without
  3595.              evaluating  the  conditional  again.   The  continue
  3596.              block, if any, is not executed.   If  the  LABEL  is
  3597.              omitted, the command refers to the innermost enclos-
  3598.              ing loop.  This command is normally used by programs
  3599.              that  want  to lie to themselves about what was just
  3600.              input:
  3601.  
  3602.                   # a simpleminded Pascal comment stripper
  3603.                   # (warning: assumes no { or } in strings)
  3604.                   line: while (<STDIN>) {
  3605.                        while (s|({.*}.*){.*}|$1 |) {}
  3606.                        s|{.*}| |;
  3607.                        if (s|{.*| |) {
  3608.                             $front = $_;
  3609.                             while (<STDIN>) {
  3610.                                  if (/}/) {     # end of comment?
  3611.                                       s|^|$front{|;
  3612.                                       redo line;
  3613.                                  }
  3614.                             }
  3615.                        }
  3616.                        print;
  3617.                   }
  3618.  
  3619.  
  3620.      rename(OLDNAME,NEWNAME)
  3621.              Changes the name of a file.  Returns 1 for  success,
  3622.              0  otherwise.  Will not work across filesystem boun-
  3623.              daries.
  3624.  
  3625.  
  3626.  
  3627. Sun Release 4.1           Last change:                         55
  3628.  
  3629.  
  3630.  
  3631.  
  3632.  
  3633.  
  3634. PERL(1)                  USER COMMANDS                    PERL(1)
  3635.  
  3636.  
  3637.  
  3638.      require(EXPR)
  3639.  
  3640.      require EXPR
  3641.  
  3642.      require Includes the library file specified by EXPR,  or  by
  3643.              $_  if  EXPR is not supplied.  Has semantics similar
  3644.              to the following subroutine:
  3645.  
  3646.                   sub require {
  3647.                       local($filename) = @_;
  3648.                       return 1 if $INC{$filename};
  3649.                       local($realfilename,$result);
  3650.                       ITER: {
  3651.                        foreach $prefix (@INC) {
  3652.                            $realfilename = "$prefix/$filename";
  3653.                            if (-f $realfilename) {
  3654.                             $result = do $realfilename;
  3655.                             last ITER;
  3656.                            }
  3657.                        }
  3658.                        die "Can't find $filename in \@INC";
  3659.                       }
  3660.                       die $@ if $@;
  3661.                       die "$filename did not return true value" unless $result;
  3662.                       $INC{$filename} = $realfilename;
  3663.                       $result;
  3664.                   }
  3665.  
  3666.              Note that the file will not be included twice  under
  3667.              the same specified name.
  3668.  
  3669.      reset(EXPR)
  3670.  
  3671.      reset EXPR
  3672.  
  3673.      reset   Generally used in a continue block at the end  of  a
  3674.              loop  to  clear  variables  and reset ?? searches so
  3675.              that they work again.  The expression is interpreted
  3676.              as  a list of single characters (hyphens allowed for
  3677.              ranges).  All variables and  arrays  beginning  with
  3678.              one  of  those  letters  are reset to their pristine
  3679.              state.  If  the  expression  is  omitted,  one-match
  3680.              searches (?pattern?) are reset to match again.  Only
  3681.              resets variables or searches in the current package.
  3682.              Always returns 1.  Examples:
  3683.  
  3684.                  reset 'X';      # reset all X variables
  3685.                  reset 'a-z';    # reset lower case variables
  3686.                  reset;          # just reset ?? searches
  3687.  
  3688.              Note:  resetting  "A-Z"  is  not  recommended  since
  3689.              you'll wipe out your ARGV and ENV arrays.
  3690.  
  3691.  
  3692.  
  3693. Sun Release 4.1           Last change:                         56
  3694.  
  3695.  
  3696.  
  3697.  
  3698.  
  3699.  
  3700. PERL(1)                  USER COMMANDS                    PERL(1)
  3701.  
  3702.  
  3703.  
  3704.              The use of reset on dbm associative arrays does  not
  3705.              change  the  dbm file.  (It does, however, flush any
  3706.              entries cached by perl, which may be useful  if  you
  3707.              are sharing the dbm file.  Then again, maybe not.)
  3708.  
  3709.      return LIST
  3710.              Returns from a subroutine with the value  specified.
  3711.              (Note that a subroutine can automatically return the
  3712.              value of the last expression evaluated.  That's  the
  3713.              preferred method--use of an explicit return is a bit
  3714.              slower.)
  3715.  
  3716.      reverse(LIST)
  3717.  
  3718.      reverse LIST
  3719.              In an array context, returns an array value consist-
  3720.              ing  of  the elements of LIST in the opposite order.
  3721.              In a scalar context, returns a string value consist-
  3722.              ing of the bytes of the first element of LIST in the
  3723.              opposite order.
  3724.  
  3725.      rewinddir(DIRHANDLE)
  3726.  
  3727.      rewinddir DIRHANDLE
  3728.              Sets the current position to the  beginning  of  the
  3729.              directory for the readdir() routine on DIRHANDLE.
  3730.  
  3731.      rindex(STR,SUBSTR,POSITION)
  3732.  
  3733.      rindex(STR,SUBSTR)
  3734.              Works just like index except  that  it  returns  the
  3735.              position  of  the  LAST occurrence of SUBSTR in STR.
  3736.              If  POSITION  is   specified,   returns   the   last
  3737.              occurrence at or before that position.
  3738.  
  3739.      rmdir(FILENAME)
  3740.  
  3741.      rmdir FILENAME
  3742.              Deletes the directory specified by FILENAME if it is
  3743.              empty.   If  it  succeeds it returns 1, otherwise it
  3744.              returns 0 and sets $! (errno).  If FILENAME is omit-
  3745.              ted, uses $_.
  3746.  
  3747.      s/PATTERN/REPLACEMENT/gieo
  3748.              Searches a string  for  a  pattern,  and  if  found,
  3749.              replaces  that pattern with the replacement text and
  3750.              returns the number of substitutions made.  Otherwise
  3751.              it  returns  false (0).  The "g" is optional, and if
  3752.              present, indicates that all occurrences of the  pat-
  3753.              tern  are to be replaced.  The "i" is also optional,
  3754.              and if present, indicates that  matching  is  to  be
  3755.              done  in  a  case-insensitive  manner.   The  "e" is
  3756.  
  3757.  
  3758.  
  3759. Sun Release 4.1           Last change:                         57
  3760.  
  3761.  
  3762.  
  3763.  
  3764.  
  3765.  
  3766. PERL(1)                  USER COMMANDS                    PERL(1)
  3767.  
  3768.  
  3769.  
  3770.              likewise optional, and if  present,  indicates  that
  3771.              the  replacement  string  is  to  be evaluated as an
  3772.              expression  rather  than  just  as  a  double-quoted
  3773.              string.   Any non-alphanumeric delimiter may replace
  3774.              the slashes; if single quotes are used, no interpre-
  3775.              tation  is  done  on  the  replacement string (the e
  3776.              modifier overrides this, however); if backquotes are
  3777.              used, the replacement string is a command to execute
  3778.              whose output will be used as the actual  replacement
  3779.              text.   If  no  string is specified via the =~ or !~
  3780.              operator, the $_ string is  searched  and  modified.
  3781.              (The string specified with =~ must be a scalar vari-
  3782.              able, an array element, or an assignment to  one  of
  3783.              those,  i.e. an lvalue.) If the pattern contains a $
  3784.              that looks like a variable rather  than  an  end-of-
  3785.              string  test, the variable will be interpolated into
  3786.              the pattern at run-time.  If you only want the  pat-
  3787.              tern  compiled  once  the first time the variable is
  3788.              interpolated, add an "o" at the end.  See  also  the
  3789.              section on regular expressions.  Examples:
  3790.  
  3791.                  s/\bgreen\b/mauve/g;      # don't change wintergreen
  3792.  
  3793.                  $path =~ s|/usr/bin|/usr/local/bin|;
  3794.  
  3795.                  s/Login: $foo/Login: $bar/; # run-time pattern
  3796.  
  3797.                  ($foo = $bar) =~ s/bar/foo/;
  3798.  
  3799.                  $_ = 'abc123xyz';
  3800.                  s/\d+/$&*2/e;        # yields 'abc246xyz'
  3801.                  s/\d+/sprintf("%5d",$&)/e;     # yields 'abc  246xyz'
  3802.                  s/\w/$& x 2/eg;      # yields 'aabbcc  224466xxyyzz'
  3803.  
  3804.                  s/([^ ]*) *([^ ]*)/$2 $1/;     # reverse 1st two fields
  3805.  
  3806.              (Note the use of $ instead of \ in the last example.
  3807.              See section on regular expressions.)
  3808.  
  3809.      scalar(EXPR)
  3810.              Forces EXPR to be interpreted in  a  scalar  context
  3811.              and returns the value of EXPR.
  3812.  
  3813.      seek(FILEHANDLE,POSITION,WHENCE)
  3814.              Randomly positions the file pointer for  FILEHANDLE,
  3815.              just like the fseek() call of stdio.  FILEHANDLE may
  3816.              be an expression whose value gives the name  of  the
  3817.              filehandle.  Returns 1 upon success, 0 otherwise.
  3818.  
  3819.      seekdir(DIRHANDLE,POS)
  3820.              Sets the current position for the readdir()  routine
  3821.              on  DIRHANDLE.   POS  must  be  a  value returned by
  3822.  
  3823.  
  3824.  
  3825. Sun Release 4.1           Last change:                         58
  3826.  
  3827.  
  3828.  
  3829.  
  3830.  
  3831.  
  3832. PERL(1)                  USER COMMANDS                    PERL(1)
  3833.  
  3834.  
  3835.  
  3836.              telldir().  Has  the  same  caveats  about  possible
  3837.              directory  compaction  as  the  corresponding system
  3838.              library routine.
  3839.  
  3840.      select(FILEHANDLE)
  3841.  
  3842.      select  Returns the currently selected filehandle.  Sets the
  3843.              current default filehandle for output, if FILEHANDLE
  3844.              is supplied.  This has two effects: first,  a  write
  3845.              or a print without a filehandle will default to this
  3846.              FILEHANDLE.  Second, references to variables related
  3847.              to  output  will  refer to this output channel.  For
  3848.              example, if you have to set the top of  form  format
  3849.              for  more  than one output channel, you might do the
  3850.              following:
  3851.  
  3852.                   select(REPORT1);
  3853.                   $^ = 'report1_top';
  3854.                   select(REPORT2);
  3855.                   $^ = 'report2_top';
  3856.  
  3857.              FILEHANDLE may be an expression  whose  value  gives
  3858.              the name of the actual filehandle.  Thus:
  3859.  
  3860.                   $oldfh = select(STDERR); $| = 1; select($oldfh);
  3861.  
  3862.  
  3863.      select(RBITS,WBITS,EBITS,TIMEOUT)
  3864.              This calls the select system call with the  bitmasks
  3865.              specified,  which  can be constructed using fileno()
  3866.              and vec(), along these lines:
  3867.  
  3868.                   $rin = $win = $ein = '';
  3869.                   vec($rin,fileno(STDIN),1) = 1;
  3870.                   vec($win,fileno(STDOUT),1) = 1;
  3871.                   $ein = $rin | $win;
  3872.  
  3873.              If you want to select on many filehandles you  might
  3874.              wish to write a subroutine:
  3875.  
  3876.                   sub fhbits {
  3877.                       local(@fhlist) = split(' ',$_[0]);
  3878.                       local($bits);
  3879.                       for (@fhlist) {
  3880.                        vec($bits,fileno($_),1) = 1;
  3881.                       }
  3882.                       $bits;
  3883.                   }
  3884.                   $rin = &fhbits('STDIN TTY SOCK');
  3885.  
  3886.              The usual idiom is:
  3887.  
  3888.  
  3889.  
  3890.  
  3891. Sun Release 4.1           Last change:                         59
  3892.  
  3893.  
  3894.  
  3895.  
  3896.  
  3897.  
  3898. PERL(1)                  USER COMMANDS                    PERL(1)
  3899.  
  3900.  
  3901.  
  3902.                   ($nfound,$timeleft) =
  3903.                     select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
  3904.  
  3905.              or to block until something becomes ready:
  3906.  
  3907.                   $nfound = select($rout=$rin, $wout=$win,
  3908.                                  $eout=$ein, undef);
  3909.  
  3910.              Any of the bitmasks can also be undef.  The timeout,
  3911.              if  specified,  is  in  seconds,  which may be frac-
  3912.              tional.  NOTE: not all implementations  are  capable
  3913.              of  returning  the  $timeleft.   If not, they always
  3914.              return $timeleft equal to the supplied $timeout.
  3915.  
  3916.      semctl(ID,SEMNUM,CMD,ARG)
  3917.              Calls the System V IPC function semctl.  If  CMD  is
  3918.              &IPC_STAT  or  &GETALL,  then ARG must be a variable
  3919.              which will hold the returned semid_ds  structure  or
  3920.              semaphore  value  array.   Returns  like  ioctl: the
  3921.              undefined value for error, "0 but true" for zero, or
  3922.              the actual return value otherwise.
  3923.  
  3924.      semget(KEY,NSEMS,SIZE,FLAGS)
  3925.              Calls the System V IPC function semget.  Returns the
  3926.              semaphore  id, or the undefined value if there is an
  3927.              error.
  3928.  
  3929.      semop(KEY,OPSTRING)
  3930.              Calls the System V IPC  function  semop  to  perform
  3931.              semaphore  operations such as signaling and waiting.
  3932.              OPSTRING must be a packed array of semop structures.
  3933.              Each   semop   structure   can   be  generated  with
  3934.              'pack("sss",  $semnum,  $semop,   $semflag)'.    The
  3935.              number  of  semaphore  operations  is implied by the
  3936.              length of OPSTRING.  Returns true if successful,  or
  3937.              false if there is an error.  As an example, the fol-
  3938.              lowing code waits on semaphore $semnum of  semaphore
  3939.              id $semid:
  3940.  
  3941.                   $semop = pack("sss", $semnum, -1, 0);
  3942.                   die "Semaphore trouble: $!\n" unless semop($semid, $semop);
  3943.  
  3944.              To signal the semaphore, replace "-1" with "1".
  3945.  
  3946.      send(SOCKET,MSG,FLAGS,TO)
  3947.  
  3948.      send(SOCKET,MSG,FLAGS)
  3949.              Sends a message on a socket.  Takes the  same  flags
  3950.              as the system call of the same name.  On unconnected
  3951.              sockets you must specify a destination to  send  TO.
  3952.              Returns  the number of characters sent, or the unde-
  3953.              fined value if there is an error.
  3954.  
  3955.  
  3956.  
  3957. Sun Release 4.1           Last change:                         60
  3958.  
  3959.  
  3960.  
  3961.  
  3962.  
  3963.  
  3964. PERL(1)                  USER COMMANDS                    PERL(1)
  3965.  
  3966.  
  3967.  
  3968.      setpgrp(PID,PGRP)
  3969.              Sets the current process  group  for  the  specified
  3970.              PID,  0  for  the  current  process.  Will produce a
  3971.              fatal error if used on a machine that doesn't imple-
  3972.              ment setpgrp(2).
  3973.  
  3974.      setpriority(WHICH,WHO,PRIORITY)
  3975.              Sets the current priority for a process,  a  process
  3976.              group,  or  a user.  (See setpriority(2).) Will pro-
  3977.              duce a fatal error if used on a machine that doesn't
  3978.              implement setpriority(2).
  3979.  
  3980.      setsockopt(SOCKET,LEVEL,OPTNAME,OPTVAL)
  3981.              Sets the socket option requested.  Returns undefined
  3982.              if  there  is  an error.  OPTVAL may be specified as
  3983.              undef if you don't want to pass an argument.
  3984.  
  3985.      shift(ARRAY)
  3986.  
  3987.      shift ARRAY
  3988.  
  3989.      shift   Shifts the first value of the array off and  returns
  3990.              it,  shortening the array by 1 and moving everything
  3991.              down.  If  there  are  no  elements  in  the  array,
  3992.              returns  the  undefined value.  If ARRAY is omitted,
  3993.              shifts the @ARGV array in the main program, and  the
  3994.              @_  array in subroutines.  (This is determined lexi-
  3995.              cally.)  See  also  unshift(),  push()  and   pop().
  3996.              Shift()  and unshift() do the same thing to the left
  3997.              end of an array that push()  and  pop()  do  to  the
  3998.              right end.
  3999.  
  4000.      shmctl(ID,CMD,ARG)
  4001.              Calls the System V IPC function shmctl.  If  CMD  is
  4002.              &IPC_STAT,  then  ARG  must be a variable which will
  4003.              hold the returned shmid_ds structure.  Returns  like
  4004.              ioctl:  the  undefined value for error, "0 but true"
  4005.              for zero, or the actual return value otherwise.
  4006.  
  4007.      shmget(KEY,SIZE,FLAGS)
  4008.              Calls the System V IPC function shmget.  Returns the
  4009.              shared  memory segment id, or the undefined value if
  4010.              there is an error.
  4011.  
  4012.      shmread(ID,VAR,POS,SIZE)
  4013.  
  4014.      shmwrite(ID,STRING,POS,SIZE)
  4015.              Reads or writes the System V shared  memory  segment
  4016.              ID starting at position POS for size SIZE by attach-
  4017.              ing to it, copying in/out, and  detaching  from  it.
  4018.              When reading, VAR must be a variable which will hold
  4019.              the data read.  When writing, if STRING is too long,
  4020.  
  4021.  
  4022.  
  4023. Sun Release 4.1           Last change:                         61
  4024.  
  4025.  
  4026.  
  4027.  
  4028.  
  4029.  
  4030. PERL(1)                  USER COMMANDS                    PERL(1)
  4031.  
  4032.  
  4033.  
  4034.              only  SIZE  bytes  are used; if STRING is too short,
  4035.              nulls are written to fill out  SIZE  bytes.   Return
  4036.              true if successful, or false if there is an error.
  4037.  
  4038.      shutdown(SOCKET,HOW)
  4039.              Shuts down a socket connection in the  manner  indi-
  4040.              cated  by  HOW, which has the same interpretation as
  4041.              in the system call of the same name.
  4042.  
  4043.      sin(EXPR)
  4044.  
  4045.      sin EXPR
  4046.              Returns the sine of EXPR (expressed in radians).  If
  4047.              EXPR is omitted, returns sine of $_.
  4048.  
  4049.      sleep(EXPR)
  4050.  
  4051.      sleep EXPR
  4052.  
  4053.      sleep   Causes the script to sleep for EXPR seconds, or for-
  4054.              ever  if no EXPR.  May be interrupted by sending the
  4055.              process a SIGALARM.  Returns the number  of  seconds
  4056.              actually slept.
  4057.  
  4058.      socket(SOCKET,DOMAIN,TYPE,PROTOCOL)
  4059.              Opens a socket of the specified kind and attaches it
  4060.              to filehandle SOCKET.  DOMAIN, TYPE and PROTOCOL are
  4061.              specified the same as for the  system  call  of  the
  4062.              same name.  You may need to run h2ph on sys/socket.h
  4063.              to get the proper values handy  in  a  perl  library
  4064.              file.   Return  true if successful.  See the example
  4065.              in the section on Interprocess Communication.
  4066.  
  4067.      socketpair(SOCKET1,SOCKET2,DOMAIN,TYPE,PROTOCOL)
  4068.              Creates an unnamed pair of sockets in the  specified
  4069.              domain,  of  the  specified  type.  DOMAIN, TYPE and
  4070.              PROTOCOL are specified the same as  for  the  system
  4071.              call  of  the same name.  If unimplemented, yields a
  4072.              fatal error.  Return true if successful.
  4073.  
  4074.      sort(SUBROUTINE LIST)
  4075.  
  4076.      sort(LIST)
  4077.  
  4078.      sort SUBROUTINE LIST
  4079.  
  4080.      sort LIST
  4081.              Sorts the LIST and returns the sorted  array  value.
  4082.              Nonexistent  values  of arrays are stripped out.  If
  4083.              SUBROUTINE is omitted, sorts in standard string com-
  4084.              parison  order.   If  SUBROUTINE is specified, gives
  4085.              the name of a subroutine  that  returns  an  integer
  4086.  
  4087.  
  4088.  
  4089. Sun Release 4.1           Last change:                         62
  4090.  
  4091.  
  4092.  
  4093.  
  4094.  
  4095.  
  4096. PERL(1)                  USER COMMANDS                    PERL(1)
  4097.  
  4098.  
  4099.  
  4100.              less than, equal to, or greater than 0, depending on
  4101.              how the elements of the array are to be ordered.  In
  4102.              the  interests of efficiency the normal calling code
  4103.              for subroutines  is  bypassed,  with  the  following
  4104.              effects:  the subroutine may not be a recursive sub-
  4105.              routine, and the two elements  to  be  compared  are
  4106.              passed  into the subroutine not via @_ but as $a and
  4107.              $b (see example below).  They are passed  by  refer-
  4108.              ence so don't modify $a and $b.  SUBROUTINE may be a
  4109.              scalar variable name, in which case the  value  pro-
  4110.              vides the name of the subroutine to use.  Examples:
  4111.  
  4112.                   sub byage {
  4113.                       $age{$a} - $age{$b}; # presuming integers
  4114.                   }
  4115.                   @sortedclass = sort byage @class;
  4116.  
  4117.                   sub reverse { $a lt $b ? 1 : $a gt $b ? -1 : 0; }
  4118.                   @harry = ('dog','cat','x','Cain','Abel');
  4119.                   @george = ('gone','chased','yz','Punished','Axed');
  4120.                   print sort @harry;
  4121.                        # prints AbelCaincatdogx
  4122.                   print sort reverse @harry;
  4123.                        # prints xdogcatCainAbel
  4124.                   print sort @george, 'to', @harry;
  4125.                        # prints AbelAxedCainPunishedcatchaseddoggonetoxyz
  4126.  
  4127.  
  4128.      splice(ARRAY,OFFSET,LENGTH,LIST)
  4129.  
  4130.      splice(ARRAY,OFFSET,LENGTH)
  4131.  
  4132.      splice(ARRAY,OFFSET)
  4133.              Removes the elements designated by OFFSET and LENGTH
  4134.              from  an  array, and replaces them with the elements
  4135.              of LIST, if any.  Returns the elements removed  from
  4136.              the array.  The array grows or shrinks as necessary.
  4137.              If LENGTH is omitted, removes everything from OFFSET
  4138.              onward.   The following equivalencies hold (assuming
  4139.              $[ == 0):
  4140.  
  4141.                   push(@a,$x,$y)                splice(@a,$#a+1,0,$x,$y)
  4142.                   pop(@a)                       splice(@a,-1)
  4143.                   shift(@a)                     splice(@a,0,1)
  4144.                   unshift(@a,$x,$y)             splice(@a,0,0,$x,$y)
  4145.                   $a[$x] = $y                   splice(@a,$x,1,$y);
  4146.  
  4147.              Example, assuming array lengths are passed before arrays:
  4148.  
  4149.                   sub aeq { # compare two array values
  4150.                        local(@a) = splice(@_,0,shift);
  4151.                        local(@b) = splice(@_,0,shift);
  4152.  
  4153.  
  4154.  
  4155. Sun Release 4.1           Last change:                         63
  4156.  
  4157.  
  4158.  
  4159.  
  4160.  
  4161.  
  4162. PERL(1)                  USER COMMANDS                    PERL(1)
  4163.  
  4164.  
  4165.  
  4166.                        return 0 unless @a == @b;     # same len?
  4167.                        while (@a) {
  4168.                            return 0 if pop(@a) ne pop(@b);
  4169.                        }
  4170.                        return 1;
  4171.                   }
  4172.                   if (&aeq($len,@foo[1..$len],0+@bar,@bar)) { ... }
  4173.  
  4174.  
  4175.      split(/PATTERN/,EXPR,LIMIT)
  4176.  
  4177.      split(/PATTERN/,EXPR)
  4178.  
  4179.      split(/PATTERN/)
  4180.  
  4181.      split   Splits a  string  into  an  array  of  strings,  and
  4182.              returns  it.   (If  not in an array context, returns
  4183.              the number of fields found and splits  into  the  @_
  4184.              array.   (In  an  array  context,  you can force the
  4185.              split into @_ by using ?? as the pattern delimiters,
  4186.              but  it  still returns the array value.)) If EXPR is
  4187.              omitted, splits the $_ string.  If PATTERN  is  also
  4188.              omitted,  splits  on  whitespace (/[ \t\n]+/).  Any-
  4189.              thing matching PATTERN is taken to  be  a  delimiter
  4190.              separating the fields.  (Note that the delimiter may
  4191.              be longer than one character.) If  LIMIT  is  speci-
  4192.              fied,  splits  into  no  more  than that many fields
  4193.              (though it may  split  into  fewer).   If  LIMIT  is
  4194.              unspecified,   trailing  null  fields  are  stripped
  4195.              (which potential users of pop()  would  do  well  to
  4196.              remember).   A pattern matching the null string (not
  4197.              to be confused with a null pattern //, which is just
  4198.              one  member  of  the set of patterns matching a null
  4199.              string) will split the value of EXPR  into  separate
  4200.              characters  at  each point it matches that way.  For
  4201.              example:
  4202.  
  4203.                   print join(':', split(/ */, 'hi there'));
  4204.  
  4205.              produces the output 'h:i:t:h:e:r:e'.
  4206.  
  4207.              The LIMIT parameter can be used to partially split a
  4208.              line
  4209.  
  4210.                   ($login, $passwd, $remainder) = split(/:/, $_, 3);
  4211.  
  4212.              (When assigning to a list, if LIMIT is omitted, perl
  4213.              supplies a LIMIT one larger than the number of vari-
  4214.              ables in the list, to avoid unnecessary  work.   For
  4215.              the  list  above LIMIT would have been 4 by default.
  4216.              In time critical applications it behooves you not to
  4217.              split into more fields than you really need.)
  4218.  
  4219.  
  4220.  
  4221. Sun Release 4.1           Last change:                         64
  4222.  
  4223.  
  4224.  
  4225.  
  4226.  
  4227.  
  4228. PERL(1)                  USER COMMANDS                    PERL(1)
  4229.  
  4230.  
  4231.  
  4232.              If  the  PATTERN  contains  parentheses,  additional
  4233.              array  elements  are created from each matching sub-
  4234.              string in the delimiter.
  4235.  
  4236.                   split(/([,-])/,"1-10,20");
  4237.  
  4238.              produces the array value
  4239.  
  4240.                   (1,'-',10,',',20)
  4241.  
  4242.              The  pattern  /PATTERN/  may  be  replaced  with  an
  4243.              expression to specify patterns that vary at runtime.
  4244.              (To  do   runtime   compilation   only   once,   use
  4245.              /$variable/o.) As a special case, specifying a space
  4246.              (' ') will split on white space just as  split  with
  4247.              no  arguments does, but leading white space does NOT
  4248.              produce a null first field.  Thus, split(' ') can be
  4249.              used  to  emulate  awk's  default  behavior, whereas
  4250.              split(/ /) will give you as many null initial fields
  4251.              as there are leading spaces.
  4252.  
  4253.              Example:
  4254.  
  4255.                   open(passwd, '/etc/passwd');
  4256.                   while (<passwd>) {
  4257.                        ($login, $passwd, $uid, $gid, $gcos, $home, $shell)
  4258.                             = split(/:/);
  4259.                        ...
  4260.                   }
  4261.  
  4262.              (Note that $shell above will still have a newline on
  4263.              it.  See chop().) See also join.
  4264.  
  4265.      sprintf(FORMAT,LIST)
  4266.              Returns a string formatted by the usual printf  con-
  4267.              ventions.  The * character is not supported.
  4268.  
  4269.      sqrt(EXPR)
  4270.  
  4271.      sqrt EXPR
  4272.              Return the square root of EXPR.  If EXPR is omitted,
  4273.              returns square root of $_.
  4274.  
  4275.      srand(EXPR)
  4276.  
  4277.      srand EXPR
  4278.              Sets the random number seed for the  rand  operator.
  4279.              If EXPR is omitted, does srand(time).
  4280.  
  4281.  
  4282.  
  4283.  
  4284.  
  4285.  
  4286.  
  4287. Sun Release 4.1           Last change:                         65
  4288.  
  4289.  
  4290.  
  4291.  
  4292.  
  4293.  
  4294. PERL(1)                  USER COMMANDS                    PERL(1)
  4295.  
  4296.  
  4297.  
  4298.      stat(FILEHANDLE)
  4299.  
  4300.      stat FILEHANDLE
  4301.  
  4302.      stat(EXPR)
  4303.  
  4304.      stat SCALARVARIABLE
  4305.              Returns a 13-element array giving the statistics for
  4306.              a  file,  either  the file opened via FILEHANDLE, or
  4307.              named by EXPR.  Typically used as follows:
  4308.  
  4309.                  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
  4310.                     $atime,$mtime,$ctime,$blksize,$blocks)
  4311.                         = stat($filename);
  4312.  
  4313.              If stat is passed the special filehandle  consisting
  4314.              of  an  underline,  no stat is done, but the current
  4315.              contents of the stat structure from the last stat or
  4316.              filetest are returned.  Example:
  4317.  
  4318.                   if (-x $file && (($d) = stat(_)) && $d < 0) {
  4319.                        print "$file is executable NFS file\n";
  4320.                   }
  4321.  
  4322.  
  4323.      study(SCALAR)
  4324.  
  4325.      study SCALAR
  4326.  
  4327.      study   Takes extra time to study SCALAR ($_ if unspecified)
  4328.              in anticipation of doing many pattern matches on the
  4329.              string before it is next modified.  This may or  may
  4330.              not save time, depending on the nature and number of
  4331.              patterns you are searching on, and on the  distribu-
  4332.              tion  of  character  frequencies in the string to be
  4333.              searched--you probably want to compare runtimes with
  4334.              and  without  it  to  see  which runs faster.  Those
  4335.              loops which scan for  many  short  constant  strings
  4336.              (including  the  constant parts of more complex pat-
  4337.              terns) will benefit most.  You  may  have  only  one
  4338.              study  active  at  a  time--if you study a different
  4339.              scalar the first is  "unstudied".   (The  way  study
  4340.              works  is  this: a linked list of every character in
  4341.              the string to be searched is made, so we  know,  for
  4342.              example,  where  all  the  'k' characters are.  From
  4343.              each  search  string,  the   rarest   character   is
  4344.              selected, based on some static frequency tables con-
  4345.              structed from some  C  programs  and  English  text.
  4346.              Only those places that contain this "rarest" charac-
  4347.              ter are examined.)
  4348.  
  4349.              For example, here is  a  loop  which  inserts  index
  4350.  
  4351.  
  4352.  
  4353. Sun Release 4.1           Last change:                         66
  4354.  
  4355.  
  4356.  
  4357.  
  4358.  
  4359.  
  4360. PERL(1)                  USER COMMANDS                    PERL(1)
  4361.  
  4362.  
  4363.  
  4364.              producing  entries before any line containing a cer-
  4365.              tain pattern:
  4366.  
  4367.                   while (<>) {
  4368.                        study;
  4369.                        print ".IX foo\n" if /\bfoo\b/;
  4370.                        print ".IX bar\n" if /\bbar\b/;
  4371.                        print ".IX blurfl\n" if /\bblurfl\b/;
  4372.                        ...
  4373.                        print;
  4374.                   }
  4375.  
  4376.              In searching for /\bfoo\b/, only those locations  in
  4377.              $_  that  contain 'f' will be looked at, because 'f'
  4378.              is rarer than 'o'.  In general, this is  a  big  win
  4379.              except  in pathological cases.  The only question is
  4380.              whether it saves you more time than it took to build
  4381.              the linked list in the first place.
  4382.  
  4383.              Note that if you have to look for strings  that  you
  4384.              don't  know  till  runtime,  you can build an entire
  4385.              loop as a string and eval that to avoid  recompiling
  4386.              all  your patterns all the time.  Together with set-
  4387.              ting $/ to input entire files as  one  record,  this
  4388.              can be very fast, often faster than specialized pro-
  4389.              grams like fgrep.  The following  scans  a  list  of
  4390.              files  (@files)  for  a  list of words (@words), and
  4391.              prints out the names of those files that  contain  a
  4392.              match:
  4393.  
  4394.                   $search = 'while (<>) { study;';
  4395.                   foreach $word (@words) {
  4396.                       $search .= "++\$seen{\$ARGV} if /\b$word\b/;\n";
  4397.                   }
  4398.                   $search .= "}";
  4399.                   @ARGV = @files;
  4400.                   $/ = "\177";        # something that doesn't occur
  4401.                   eval $search;       # this screams
  4402.                   $/ = "\n";          # put back to normal input delim
  4403.                   foreach $file (sort keys(%seen)) {
  4404.                       print $file, "\n";
  4405.                   }
  4406.  
  4407.  
  4408.      substr(EXPR,OFFSET,LEN)
  4409.  
  4410.      substr(EXPR,OFFSET)
  4411.              Extracts a substring out of  EXPR  and  returns  it.
  4412.              First  character  is at offset 0, or whatever you've
  4413.              set $[ to.  If OFFSET is negative, starts  that  far
  4414.              from  the  end  of  the  string.  If LEN is omitted,
  4415.              returns everything to the end of  the  string.   You
  4416.  
  4417.  
  4418.  
  4419. Sun Release 4.1           Last change:                         67
  4420.  
  4421.  
  4422.  
  4423.  
  4424.  
  4425.  
  4426. PERL(1)                  USER COMMANDS                    PERL(1)
  4427.  
  4428.  
  4429.  
  4430.              can use the substr() function as an lvalue, in which
  4431.              case EXPR must be an lvalue.  If  you  assign  some-
  4432.              thing  shorter than LEN, the string will shrink, and
  4433.              if you assign something longer than LEN, the  string
  4434.              will grow to accommodate it.  To keep the string the
  4435.              same length you may need to pad or chop  your  value
  4436.              using sprintf().
  4437.  
  4438.      symlink(OLDFILE,NEWFILE)
  4439.              Creates a new filename symbolically  linked  to  the
  4440.              old  filename.   Returns 1 for success, 0 otherwise.
  4441.              On systems that don't support symbolic  links,  pro-
  4442.              duces a fatal error at run time.  To check for that,
  4443.              use eval:
  4444.  
  4445.                   $symlink_exists = (eval 'symlink("","");', $@ eq '');
  4446.  
  4447.  
  4448.      syscall(LIST)
  4449.  
  4450.      syscall LIST
  4451.              Calls the system call specified as the first element
  4452.              of the list, passing the remaining elements as argu-
  4453.              ments to the system call.   If  unimplemented,  pro-
  4454.              duces  a fatal error.  The arguments are interpreted
  4455.              as follows: if a  given  argument  is  numeric,  the
  4456.              argument  is  passed as an int.  If not, the pointer
  4457.              to the string value is passed.  You are  responsible
  4458.              to make sure a string is pre-extended long enough to
  4459.              receive any result that  might  be  written  into  a
  4460.              string.   If your integer arguments are not literals
  4461.              and have never been interpreted in  a  numeric  con-
  4462.              text, you may need to add 0 to them to force them to
  4463.              look like numbers.
  4464.  
  4465.                   require 'syscall.ph';         # may need to run h2ph
  4466.                   syscall(&SYS_write, fileno(STDOUT), "hi there\n", 9);
  4467.  
  4468.  
  4469.      sysread(FILEHANDLE,SCALAR,LENGTH,OFFSET)
  4470.  
  4471.      sysread(FILEHANDLE,SCALAR,LENGTH)
  4472.              Attempts to read LENGTH bytes of data into  variable
  4473.              SCALAR from the specified FILEHANDLE, using the sys-
  4474.              tem call read(2).  It bypasses stdio, so mixing this
  4475.              with  other  kinds  of  reads  may  cause confusion.
  4476.              Returns the number of bytes actually read, or  undef
  4477.              if  there  was  an  error.   SCALAR will be grown or
  4478.              shrunk to the length actually read.  An  OFFSET  may
  4479.              be  specified  to  place the read data at some other
  4480.              place than the beginning of the string.
  4481.  
  4482.  
  4483.  
  4484.  
  4485. Sun Release 4.1           Last change:                         68
  4486.  
  4487.  
  4488.  
  4489.  
  4490.  
  4491.  
  4492. PERL(1)                  USER COMMANDS                    PERL(1)
  4493.  
  4494.  
  4495.  
  4496.      system(LIST)
  4497.  
  4498.      system LIST
  4499.              Does exactly the same thing as  "exec  LIST"  except
  4500.              that  a  fork  is done first, and the parent process
  4501.              waits for the child process to complete.  Note  that
  4502.              argument  processing  varies depending on the number
  4503.              of arguments.  The return value is the  exit  status
  4504.              of  the  program as returned by the wait() call.  To
  4505.              get the actual exit value divide by 256.   See  also
  4506.              exec.
  4507.  
  4508.      syswrite(FILEHANDLE,SCALAR,LENGTH,OFFSET)
  4509.  
  4510.      syswrite(FILEHANDLE,SCALAR,LENGTH)
  4511.              Attempts to write LENGTH bytes of data from variable
  4512.              SCALAR to the specified FILEHANDLE, using the system
  4513.              call write(2).  It bypasses stdio,  so  mixing  this
  4514.              with prints may cause confusion.  Returns the number
  4515.              of bytes actually written, or undef if there was  an
  4516.              error.  An OFFSET may be specified to place the read
  4517.              data at some other place than the beginning  of  the
  4518.              string.
  4519.  
  4520.      tell(FILEHANDLE)
  4521.  
  4522.      tell FILEHANDLE
  4523.  
  4524.      tell    Returns the current file  position  for  FILEHANDLE.
  4525.              FILEHANDLE  may  be  an expression whose value gives
  4526.              the name of the actual filehandle.  If FILEHANDLE is
  4527.              omitted, assumes the file last read.
  4528.  
  4529.      telldir(DIRHANDLE)
  4530.  
  4531.      telldir DIRHANDLE
  4532.              Returns the current position of the  readdir()  rou-
  4533.              tines on DIRHANDLE.  Value may be given to seekdir()
  4534.              to access a particular location in a directory.  Has
  4535.              the same caveats about possible directory compaction
  4536.              as the corresponding system library routine.
  4537.  
  4538.      time    Returns  the  number  of  non-leap   seconds   since
  4539.              00:00:00 UTC, January 1, 1970.  Suitable for feeding
  4540.              to gmtime() and localtime().
  4541.  
  4542.      times   Returns a four-element array  giving  the  user  and
  4543.              system  times,  in seconds, for this process and the
  4544.              children of this process.
  4545.  
  4546.                  ($user,$system,$cuser,$csystem) = times;
  4547.  
  4548.  
  4549.  
  4550.  
  4551. Sun Release 4.1           Last change:                         69
  4552.  
  4553.  
  4554.  
  4555.  
  4556.  
  4557.  
  4558. PERL(1)                  USER COMMANDS                    PERL(1)
  4559.  
  4560.  
  4561.  
  4562.      tr/SEARCHLIST/REPLACEMENTLIST/cds
  4563.  
  4564.      y/SEARCHLIST/REPLACEMENTLIST/cds
  4565.              Translates all occurrences of the  characters  found
  4566.              in  the search list with the corresponding character
  4567.              in the replacement list.  It returns the  number  of
  4568.              characters  replaced  or  deleted.   If no string is
  4569.              specified via the =~ or !~ operator, the  $_  string
  4570.              is  translated.   (The string specified with =~ must
  4571.              be a  scalar  variable,  an  array  element,  or  an
  4572.              assignment to one of those, i.e. an lvalue.) For sed
  4573.              devotees, y is provided as a synonym for tr.
  4574.  
  4575.              If the c modifier is specified, the SEARCHLIST char-
  4576.              acter  set  is  complemented.   If the d modifier is
  4577.              specified, any characters  specified  by  SEARCHLIST
  4578.              that  are  not found in REPLACEMENTLIST are deleted.
  4579.              (Note that this is slightly more flexible  than  the
  4580.              behavior  of some tr programs, which delete anything
  4581.              they find in  the  SEARCHLIST,  period.)  If  the  s
  4582.              modifier  is specified, sequences of characters that
  4583.              were translated to the same character  are  squashed
  4584.              down to 1 instance of the character.
  4585.  
  4586.              If the d modifier was used, the  REPLACEMENTLIST  is
  4587.              always interpreted exactly as specified.  Otherwise,
  4588.              if the REPLACEMENTLIST is  shorter  than  the  SEAR-
  4589.              CHLIST, the final character is replicated till it is
  4590.              long enough.  If the REPLACEMENTLIST  is  null,  the
  4591.